Jquery 跨域ajax请求问题
我需要从跨域获取json数据Jquery 跨域ajax请求问题,jquery,asp.net,ajax,json,jsonp,Jquery,Asp.net,Ajax,Json,Jsonp,我需要从跨域获取json数据 $.getJSON('http://xx.xx.xx.xx/SampleService/Handler.ashx?callback=?', data, function (jsonData) { alert('1'); }) .done(function () { console.log("second success"); }) .fail(function (
$.getJSON('http://xx.xx.xx.xx/SampleService/Handler.ashx?callback=?', data, function (jsonData) {
alert('1');
})
.done(function () { console.log("second success"); })
.fail(function () { console.log("error"); })
.always(function () { console.log("complete"); });
处理程序代码:
context.Response.ContentType = "application/json";
SampleService service = new SampleService();
List<List<Byte>> response = service.GetData();
string jsonData = JsonConvert.SerializeObject(response);
context.Response.Write(string.Format("{0}([{1}]);", context.Request["callback"], jsonData));
对跨域请求使用jsonp调用。用这样的东西
$.ajax({
url : "http://xx.xx.xx.xx/SampleService/Handler.ashx",
type: "GET",
dataType: "jsonp",
jsonp : "callback",
success: function(data) {alert("Success");},
error: function(data) { alert("Error"); }
});
});
echo $_GET['callback'] . "($result)";exit;
在php页面上返回如下结果
$.ajax({
url : "http://xx.xx.xx.xx/SampleService/Handler.ashx",
type: "GET",
dataType: "jsonp",
jsonp : "callback",
success: function(data) {alert("Success");},
error: function(data) { alert("Error"); }
});
});
echo $_GET['callback'] . "($result)";exit;
其中$result是您的json_编码数组。对跨域请求使用jsonp调用。用这样的东西
$.ajax({
url : "http://xx.xx.xx.xx/SampleService/Handler.ashx",
type: "GET",
dataType: "jsonp",
jsonp : "callback",
success: function(data) {alert("Success");},
error: function(data) { alert("Error"); }
});
});
echo $_GET['callback'] . "($result)";exit;
在php页面上返回如下结果
$.ajax({
url : "http://xx.xx.xx.xx/SampleService/Handler.ashx",
type: "GET",
dataType: "jsonp",
jsonp : "callback",
success: function(data) {alert("Success");},
error: function(data) { alert("Error"); }
});
});
echo $_GET['callback'] . "($result)";exit;
其中$result是json_编码的数组。您看到的jQuery19108131180874027861_1366004862133是一个自动生成的回调包装函数,当您不指定回调函数时,jQuery会附加该函数。你有回调=?。如果您可以访问正在调用的服务器端脚本,那么可以将JSON封装在一个函数中,并使用JSONP调用该函数来解决跨域问题。Ie-将回调函数命名为jsonCallback 服务器端脚本输出:
jsonCallback(
{
[insert your json code here]
}
);
然后客户端:
(函数($){
变量url='?'
进一步阅读:您看到的jQuery19108131180874027861_1366004862133是一个自动生成的回调包装函数,当您不指定回调函数时,jQuery会附加该函数。即您有callback=?。如果您有权访问正在调用的服务器端脚本,则可以将JSON包装在函数中,并使用JSONP绕过跨域问题。Ie-将回调函数命名为jsonCallback 服务器端脚本输出:
jsonCallback(
{
[insert your json code here]
}
);
然后客户端:
(函数($){
变量url='?'
进一步阅读:@chandresh您在错误函数末尾有一个尾随的
,
。这可能会导致IE浏览器出现问题。当请求完成时,您需要提供回调函数ajax
将在那里调用此函数,您可以执行您的操作want@Jai有效点已编辑问题。谢谢更正。@dianuj我的更新答案是否指向了您所演示的内容?asp.net
而不是php
尝试在该上下文中回答。@chandresh错误函数末尾有一个尾随,
。这可能会在IE浏览器中引起问题。当请求完成ajax
时,您需要提供回调
函数l这个函数在那里你做什么want@Jai有效点编辑了该问题。谢谢更正。@dianuj我的更新答案是否与您的说明相符?asp.net
notphp
请尝试在该上下文中回答。您也可以添加crossdomain:true,
。还要注意function.success()和error()自从jQuery 1.9.x for.done()和.fail()以来,它们就被弃用了。@SamDeeringjquery4u.com,在使用您的代码后,它既没有到达done
也没有到达fail
部分。另外,我得到了空响应(在Firebug的Net选项卡中),您可以添加crossdomain:true,
。还要注意function.success()和error()自从jQuery 1.9.x for.done()和.fail()以来,已被弃用。@SamDeeringjquery4u.com使用您的代码后,它既没有到达done
部分,也没有到达fail
部分。而且我得到了空响应(在Firebug的Net选项卡中)