C# AJAX调用未从WebMethod返回JSON对象
我能够使它工作(成功) 但我得到的回应是default.aspx的整个HTML AJAX:C# AJAX调用未从WebMethod返回JSON对象,c#,json,ajax,webmethod,C#,Json,Ajax,Webmethod,我能够使它工作(成功) 但我得到的回应是default.aspx的整个HTML AJAX: function CreateLottery(lottery) { debugger; // 'lottery' comes with the properties of the Lottery class $.ajax({ type: 'POST', url: 'default.aspx/Create', data: JSON.stringify({ data: lottery
function CreateLottery(lottery) {
debugger; // 'lottery' comes with the properties of the Lottery class
$.ajax({
type: 'POST',
url: 'default.aspx/Create',
data: JSON.stringify({ data: lottery }),
dataType: 'text',
success: function (data, status) {
alert(data.TotalValue + " " + status) //"undefined success"
},
error: function () {
alert("error!")
}
});
}
我在警报中获得“未定义的成功”。“数据”是整个html文档,而不是“彩票”对象
创建Web方法和彩票类:
[WebMethod]
public static Lottery Create(Lottery lottery)
{
return lottery;
}
public class Lottery
{
public string TotalValue { get; set; }
public string Players { get; set; }
}
我不知道发生了什么,WebMethod返回的是它接收到的完全相同的对象,我怎么不能在成功回调中访问它
编辑:未命中WebMethod。“ScriptManager”存在于default.aspx中,EnablePageMethods设置为true。如果我将WebMethod名称(Create)更改为anything,并在AJAX url中保留/创建,仍然会得到整个default.aspx HTML作为响应。我认为您必须考虑两件事: 首先:您必须更正内容类型标题。它应该是
application/json
,而不是text
另一个问题是[WebMethod]
需要XML。它不直接处理JSON
要让您的WebMethod返回其格式为JSON的内容,您还必须将其修饰为。
该属性允许您将响应的格式指定为JSON
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static Lottery Create(Lottery lottery)
{
// ...
}
好吧,但有一件事我不确定:虽然您可以指定
响应格式
,但我看不到指定请求格式
的方法。当您将JSON定义为响应类型时,我假设它接受JSON作为请求类型。但这只是一个假设。试试看;-) 我们不知道为什么它对您不起作用,但也许您可以尝试另一种方法。在我看来,[WebMethod]相当老套,而且显然比需要的更复杂。您可以将WebAPI服务添加到项目中,但是如果您想坚持使用“老式”web表单,那么您可以将IHttpHandler
实现为一个ashx文件,它是一个“web服务”。请注意,.aspx页面也实现了IHttpHandler
,但它们被设计为返回HTML,而处理程序则用于处理文件下载等一般请求,以及xml和json等数据
使用Handler项模板实现类似的功能,并使用ajax调用实现:
using System.IO;
using System.Web;
namespace WebApplication1
{
public class LotteryHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
using (var reader = new StreamReader(context.Request.InputStream))
{
string values = reader.ReadToEnd();
//Use Newtonsoft to deserialize to Lottery type
//do whatever with Lottery
//Use Newtonsoft to serialize Lottery again (or whatever you want to return)
context.Response.Write(your_serialized_object);
//finish the response
context.Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
**旧代码**
$.ajax({
type: 'POST',
url: 'default.aspx/Create',
data: JSON.stringify({ data: lottery }),
dataType: 'text',
success: function (data, status) {
alert(data.TotalValue + " " + status) //"undefined success"
},
error: function () {
alert("error!")
}
});
**新代码**
请参见此处的内容类型和数据类型更改
$.ajax({
type: 'POST',
url: 'default.aspx/Create',
data: JSON.stringify({ data: lottery }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data, status) {
alert(data.TotalValue + " " + status) //"undefined success"
},
error: function () {
alert("error!")
}
});
提示:抱怨落选可能只会带来更多的落选。我删除了它。你能不能帮我,而不是这种不友好的待遇?也许你对一个合法问题的否决真的会对我有所帮助。谢谢。我认为它不起作用,“整个文档”可能是404或500 http响应。您是否确实查看了
数据的内容
?html中有什么内容?我试图弄清楚你是否有自定义错误,你甚至没有点击webmethod。一个200响应只意味着一个响应已经生成并发送。这并不意味着这是你期望的反应。在web方法中放置一个断点,看看它是否命中。我尝试了你所说的一切,但没有成功。问题似乎在于WebMethod从未被击中。“ScriptManager”出现在default.aspxToo bad上:(最好同时启动fiddler来拦截http流量,看看是否有其他提示。我只是将WebMethod名称更改为一些随机名称,并保留url(/Create)在AJAX中,我仍然得到整个HTML。不幸的是,没有。使用尽可能最简单的数据(仅一个字符串),我仍然得到整个文档。我有ScriptManager。回调成功了。我缺少什么?我不敢相信我被困在了这里面。这是HTML: