使用 jquery jsonp 返回错误回调函数未被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19351934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using jquery jsonp returns error callback function was not called
提问by Jose R
I have the following working example, I'm trying to get http://detectlanguage.comvia a jquery request. Since this will be a cross-domain request I'm using jsonp.
我有以下工作示例,我正在尝试通过 jquery 请求获取http://detectlanguage.com。由于这将是一个跨域请求,因此我正在使用 jsonp。
Here is a link in MSDNwith a similar request where it's infered that jsonp
is the way to go.
这是MSDN 中的一个链接,其中有一个类似的请求,推断这jsonp
是要走的路。
Everything's fine, except the page throws an error Error: myCallback was not called
, the response I get from server is the following:
一切都很好,除了页面抛出错误Error: myCallback was not called
,我从服务器得到的响应如下:
{"data":
{"detections":[
{"language":"ca",
"isReliable":false,
"confidence":0.14992503748125938
},
{"language":"en",
"isReliable":false,
"confidence":0.00 8103727714748784
}]
}
}
I've been searching all day in stackoverflow for answers regarding jsonp but haven't got it to work yet.
我一整天都在 stackoverflow 中寻找有关 jsonp 的答案,但还没有让它起作用。
Any help is very much appreciated
很感谢任何形式的帮助
UPDATED
更新
Including AJAX call
包括 AJAX 调用
$.ajax({
url: 'http://ws.detectlanguage.com/0.2/detect',
data: {
q: $('#hi').val(),
key:'demo'
},
jsonpCallback: 'myCallback',
dataType: "jsonp",
success: myCallback,
error: function(e,i,j){
$('#results').html(j)
}
});
I also have a javascript function called myCallback
:
我还有一个 javascript 函数,名为myCallback
:
function myCallback(response){
alert(response.data)
}
回答by Saranya
The response doesnot seem to be jsonp, the jsonp response should be a javascript. Below is the code that works for me.
响应似乎不是 jsonp,jsonp 响应应该是javascript。下面是对我有用的代码。
ajax request:
阿贾克斯请求:
$.ajax({
crossDomain: true,
type:"GET",
contentType: "application/json; charset=utf-8",
async:false,
url: "http://<your service url here>/HelloWorld?callback=?",
data: {projectID:1},
dataType: "jsonp",
jsonpCallback: 'fnsuccesscallback'
});
server side code returning jsonp (c#):
返回 jsonp (c#) 的服务器端代码:
public void HelloWorld(int projectID,string callback)
{
String s = "Hello World !!";
StringBuilder sb = new StringBuilder();
JavaScriptSerializer js = new JavaScriptSerializer();
sb.Append(callback + "(");
sb.Append(js.Serialize(s));
sb.Append(");");
Context.Response.Clear();
Context.Response.ContentType = "application/json";
Context.Response.Write(sb.ToString());
Context.Response.End();
}
回答by smoore4
I also spend a very long time looking around SO for a response. The solution for me had to do with the service, not ajax calls or jQuery. Specifically, the service needs to be set up to allow cross-domain access using this setting in web.config:
我也花了很长时间环顾四周以寻求回应。我的解决方案与服务有关,而不是 ajax 调用或 jQuery。具体来说,需要使用 web.config 中的此设置将服务设置为允许跨域访问:
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
...
<endpoint address="../YourService.svc"
binding="webHttpBinding"
bindingConfiguration="webHttpBindingWithJsonP"
contract="YourServices.IYourService"
behaviorConfiguration="webBehavior" />