AJAX到Web方法不返回JSON [英] AJAX to web method not returning JSON
问题描述
我从我的js文件使用AJAX调用aspx页面的Web方法。我已经设置方法为[的WebMethod]和页面从System.Web.Ui.Page类继承。它仍然不JSON格式回到我呼吁AJAX功能。
I am calling a web method in aspx page from my js file using AJAX. I have set the method to be [WebMethod] and the page inherits from System.Web.Ui.Page class. Still it does not return the JSON format to my calling ajax function.
下面是AJAX调用的js文件:
Here is the AJAX call in js file:
$.ajax({
type: "POST",
url: "/WebServiceUtility.aspx/CustomOrderService",
data: "{'id': '2'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (message) {
ShowPopup(message);
}
});
function ShowPopup(result) {
if (result.d != "") {
request=result.d;
}
}
这里是Web方法:
And here is the web method:
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Services;
namespace SalesDesk.Global
{
public partial class WebServiceUtility : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public string CustomOrderService(string id)
{
string result;
// code logic which sets the result value
result="some value";
return result;
}
}
}
当我preSS F12在Firefox浏览器,并检查网络的请求/响应来电,我没有看到JSON标签都没有。相反,我看到的HTML标签。
When I press F12 in Firefox browser and check the Request / Response in Network calls, I do not see the JSON tab at all. Instead I see HTML tab.
我是否需要专门设置任何响应头?究竟我在这里丢失?
Do I need to set any response headers specifically? What exactly am I missing here?
编辑:找到一个解决方案。归根结底,什么工作为$ .getJSON()与一个回调函数是成功的方法调用及以下在网页中的code
Found a solution. Ultimately, what worked is $.getJSON() call with a callback function as success method and below is the code in web page
result = "...";
Response.Clear();
Response.ContentType = "application/json";
Response.Write(result);
Response.Flush();
Response.End();
感谢所有为您的宝贵建议。
Thanks all for your valuable suggestions.
推荐答案
试试这个
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string CustomOrderService(string id)
{
string result;
// code logic which sets the result value
result="some value";
return result;
}
这篇关于AJAX到Web方法不返回JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!