症状:当我发出Web服务请求时(从使用.ajax的JQuery到ASP.NET .asmx文件),如果是使用GET而不是POST,则.asmx文件始终返回XML而不是JSON.如果我将回拨转回帖子,它会像JSON一样响应.
目标:如何使用HTTP GET获取JSON而不是XML?
我已经有了相当多的谷歌搜索,这不是通常的嫌疑人,如缺少ScriptService或没有在web.config中注册处理程序.它的行为就像脚本处理程序工厂只处理帖子?请帮助我指出正确的方向!
服务器代码:
namespace mynamespace
{
/// <summary>
/// Summary description for ServiceAddresses
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class MyService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
public string HelloWorld()
{
return "Hello World at " + DateTime.Now.ToLongTimeString();
}
}
}
Run Code Online (Sandbox Code Playgroud)
客户代码:
function testhelloworld(postorget) {
var webMethod = '/servicedir/MyService.asmx/HelloWorld';
$.ajax({
type: ('GET'===postorget)?'GET':'POST',
url: webMethod,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: "{}",
success: function(msg) {
$('#info').text(msg.d);
},
error: function(xhr, …
Run Code Online (Sandbox Code Playgroud) HttpContext.Current.ApplicationInstance.CompleteRequest似乎什么都不做.我错过了什么?
例如,尽管在每个有趣的事件期间都调用了CompleteRequest,但以下所有事件仍然在一个简单的测试页面上运行.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace LifeCycle
{
public partial class _Default_NoMasterPage : System.Web.UI.Page
{
private int count = 0;
protected override void OnInit(EventArgs e)
{
nextLabel("InitBeforeBase");
base.OnInit(e);
HttpContext.Current.ApplicationInstance.CompleteRequest();
nextLabel("Init");
}
protected override void OnInitComplete(EventArgs e)
{
nextLabel("InitCompleteBeforeBase");
base.OnInitComplete(e);
HttpContext.Current.ApplicationInstance.CompleteRequest();
nextLabel("InitComplete");
}
protected override void OnLoad(EventArgs e)
{
nextLabel("OnLoadBeforeBase");
base.OnLoad(e);
HttpContext.Current.ApplicationInstance.CompleteRequest();
nextLabel("OnLoad");
}
protected override void OnLoadComplete(EventArgs e)
{
nextLabel("OnLoadCompleteBeforeBase");
base.OnLoadComplete(e);
HttpContext.Current.ApplicationInstance.CompleteRequest();
nextLabel("OnLoadComplete");
}
protected override void OnPreInit(EventArgs e) …
Run Code Online (Sandbox Code Playgroud)