Javascript JSONP对象错误
我正在尝试发出跨域请求以发送/接收一些数据。我无法通过Javascript JSONP对象错误,javascript,jquery,jsonp,Javascript,Jquery,Jsonp,我正在尝试发出跨域请求以发送/接收一些数据。我无法通过对象错误。在我得到No Transport错误之前,我添加了Query.support.cors=true解决了这个问题 var url = "http://CROSSDOMAINSERVER:PORT/Service1.svc/GetDataUsingDataContract"; $.ajax({ type: 'GET', url: url,
对象错误
。在我得到No Transport
错误之前,我添加了Query.support.cors=true代码>解决了这个问题
var url = "http://CROSSDOMAINSERVER:PORT/Service1.svc/GetDataUsingDataContract";
$.ajax({
type: 'GET',
url: url,
data: 'tool=1&product=Some%20Product&details=9&bogus=should%20not%20pass%20validation',
datatype: "jsonp",
contentType: "application/json",
success: function (response) {
alert(response.data);
},
error: function (error) {
alert(error.statusText);
}
});
如果我在浏览器中键入url:
http://CROSSDOMAINSERVER:PORT/Service1.svc/GetDataUsingDataContract?&tool=1&product=Some%20Product&details=9&bogus=should%20not%20pass%20validation
我得到了正确的回答
{"d":{"__type":"ClientSideData:#ProdResourceToolService","details":"9","product":"Some Product","result":"1","site":"Somewhere, SD","systime":"2\/6\/2013 2:50:20 PM","team":"0000000000","tool":"1","user":"username"}}
当我使用ajax时,它不会将其提交到数据库或返回数据,只是对象错误
。有没有人对如何解决这个问题有什么建议
我还应该指定是否删除http://CROSSDOMAINSERVER:PORT/
从var url
在本地调试时,我得到了正确的json响应。为什么跨域不起作用?这是您当前的回答:
{
"d": {
"__type": "ClientSideData:#ProdResourceToolService",
"details": "9",
"product": "Some Product",
"result": "1",
"site": "Somewhere, SD",
"systime": "2/6/2013 2:50:20 PM",
"team": "0000000000",
"tool": "1",
"user": "username"
}
}
这是一个有效的字符串。然而,这是无效的。如果可能,让您的服务将json包装到函数中:
responseFunc({
"d": {
"__type": "ClientSideData:#ProdResourceToolService",
"details": "9",
"product": "Some Product",
"result": "1",
"site": "Somewhere, SD",
"systime": "2/6/2013 2:50:20 PM",
"team": "0000000000",
"tool": "1",
"user": "username"
}
});
在$.ajax()
调用中,添加一个属性:jsonpCallback:'responseFunc'
另外,我建议将数据作为对象传递:
data: { tool: 1, product: 'Some Product' } //etc...
如果您可以从服务器端访问服务而不出现任何问题,那么您可以创建一个httphandler(.ashx)并让它为您生成JSON。这样,您就不必在客户端处理跨域问题。如果您真的想执行JSONP,那么这种响应是不正确的。JSONP的返回值应该是函数调用的JavaScript代码,而不仅仅是纯JSON。从问题的最后一行开始,看起来您是在同一个域中运行它!您是从http://localhost/
?jsonp是执行跨域请求的唯一方法,对吗?您从哪里想到将$.support.cors
更改为true
?如果浏览器不支持cors,那么使用cors并不会使它变得更好。如果您正在使用JSONP,则不需要cors。如果你在做CORS,你不需要JSONP。同时使用它们只是让人困惑。