在 JSONP-Ajax 请求后获取 JSON 元素时出现奇怪的错误
这是我的请求:
$.ajax({
url: 'http://www.mywebsite.com/folder/mypage.aspx',
dataType: 'jsonp',
success: function(result) {
console.log(result);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus + " - " + errorThrown);
}
});
从 mypage.aspx (在 .cs 上)我提出了这个:
Response.Write("{ html: '" + "Hello I'm a string" + "'}");
但我得到 parsererror - Error: jQuery17100985457205879069_1330089260383 was not called
,所以错误函数被调用...
在哪里我错了?
This is my request :
$.ajax({
url: 'http://www.mywebsite.com/folder/mypage.aspx',
dataType: 'jsonp',
success: function(result) {
console.log(result);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus + " - " + errorThrown);
}
});
and from mypage.aspx (on the .cs) I put out this :
Response.Write("{ html: '" + "Hello I'm a string" + "'}");
but I get parsererror - Error: jQuery17100985457205879069_1330089260383 was not called
, so error function is called...
Where am I wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有返回正确的 JSONP 结果。
您需要返回包装在函数调用中的对象,该函数调用调用查询字符串中指定的函数。另外,当您使用撇号来分隔字符串时,您必须转义字符串中的撇号:
附注:响应中的对象不是有效的 JSON,但它不一定是,因为它不会解析为 JSON,而是解析为JavaScript。您可以返回有效的 JSON 只是为了更好的衡量标准:
You are not returning a correct JSONP result.
You need to return the object wrapped in a function call, that calls the function that is specified in the query string. Also, you have to escape the apostrophe in the string as you are using apostrophes to delimit the string:
Side note: The object in the response is not valid JSON, but it doesn't have to be as it's not parsed as JSON but as Javascript. You could return valid JSON just for good measure:
您必须将您的响应放入“函数”中。它的名称在
callback
GET 参数中指定,您必须使用它。所以结果会很容易,将你的行更改为
You have to put your response into a "function". It's name is stated in
callback
GET parameter, and you have to use it. So result will beso easily, change your line to
尝试设置类似这样的内容类型
Respone.ContentType = "application/json"
try setting content type something like this
Respone.ContentType = "application/json"