Javascript 如何使用jQuery获取Ajax请求数据?
我有一个Ajax请求:Javascript 如何使用jQuery获取Ajax请求数据?,javascript,jquery,ajax,response,Javascript,Jquery,Ajax,Response,我有一个Ajax请求: $.ajax({ url: "MyPage.aspx", data: params, success: function(data) { // Check results $('#testp').append(data.message); enableForm(); }, error: function() { alert('Unable to
$.ajax({ url: "MyPage.aspx",
data: params,
success: function(data) {
// Check results
$('#testp').append(data.message);
enableForm();
},
error: function() {
alert('Unable to load the permissions for this user level.\n\nYour login may have expired.');
enableForm();
},
dataType: "json"
});
在请求页面上,有一个C#代码在加载页面末尾执行此操作:
Response.AppendHeader("X-JSON", result);
“结果”的格式如下:
{ "success": true, "message": "SUCCESS", "user_level": 25, "switches": [ { "number": 30, "is_enabled": false, "is_default": false }, { "number": 30, "is_enabled": false, "is_default": false } ]}
Response.ContentType = "application/json";
Response.Write(result);
Response.End();
请求成功返回,但“数据”为空。我错过了什么
谢谢。答案应该是:
将方法名称添加到ajax设置的url值的末尾:
url:“MyPage.aspx/method”
确保使用该方法指向正确的页面,并确保该方法具有WebMethod()属性
$.ajax几乎可以为您处理所有事情。如果AJAX调用成功,返回数据将作为参数传递给success函数,即:
success: function (data) {
//do something
}
仅当“message”是返回数据的属性时,“data.message”的值才可用。在您的例子中,id为“testp”的元素应该接收“data.message”的值,但对象的其余部分没有被使用
非常适合您查看AJAX for.Net的所有内容的博客:
我经常使用JSON库()来测试返回的数据。加上
alert(JSON.stringify(data));
为了你的成功功能我最近运气不错,这是我写的帖子,这里有很多好信息的链接
您的主要问题似乎是在HTTP头中返回JSON数据,而不是作为响应的内容。您可能希望执行以下操作:
{ "success": true, "message": "SUCCESS", "user_level": 25, "switches": [ { "number": 30, "is_enabled": false, "is_default": false }, { "number": 30, "is_enabled": false, "is_default": false } ]}
Response.ContentType = "application/json";
Response.Write(result);
Response.End();
这可能会解决您眼前的问题,但我强烈建议您避免使用ASPX页面的直接输出。当您真正想要的只是一个简单的JSON端点时,在到达页面加载点时会涉及到很多不必要的开销。更不用说,手动处理JSON序列化是没有必要的
如果要从服务器端的对象构建JSON字符串,可以使用ASP.NET AJAX“页面方法”直接返回该字符串,并让框架处理序列化。像这样:
public class PermissionsResult
{
public bool success;
public string message;
public int user_level;
public List<Switch> switches;
}
public class Switch
{
public int number;
public bool is_enabled;
public bool is_default;
}
// The combination of a WebMethod attribute and public-static declaration
// causes the framework to create a lightweight endpoint for this method that
// exists outside of the normal Page lifecycle for the ASPX page.
[WebMethod]
public static PermissionsResult GetPermissions(int UserLevel)
{
PermissionsResult result = new PermissionsResult();
// Your current business logic to populate this permissions data.
result = YourBusinessLogic.GetPermissionsByLevel(UserLevel);
// The framework will automatically JSON serialize this for you.
return result;
}
关于数据参数,以下是需要为字符串的信息:
此外,这里还有关于使用JSON.stringify()方法的更多信息:
d问题一开始可能令人困惑。基本上,JSON将以这种方式返回,而不是您可能期望的方式返回:
{"d": { "success": true, "message": "SUCCESS", "user_level": 25, "switches": [ { "number": 30, "is_enabled": false, "is_default": false }, { "number": 30, "is_enabled": false, "is_default": false } ]}}
一旦你预料到了,就很容易解释清楚。当顶级容器是数组时,它可以减少相当危险的客户端攻击,从而使端点更加安全。不适用于此特定情况,但作为一项规则,很高兴拥有。您可以在这里了解更多信息:我知道它应该做什么,但它没有做到。问题是“success:function(data){}”中的“data”是空的。为什么为空?如果“MyPage.aspx”是返回数据的方法所在的位置,并说您的方法名为“getData”,则执行以下操作:url:“MyPage.aspx/getData”。此外,参数应与方法的输入值匹配。如果没有输入变量,则使用数据:“{}”。最后,getData需要WebMethod()属性