I have a simple ajax call like this:
我有一个简单的ajax调用,如下所示:
$.ajax({
url: u, type: "POST", dataType: "json",
data: data,
success: function (d) { response($.map(d, function (o) { return { label: o.Text, value: o.Text, id: o.Id} })); }
});
It is part of an tb autocomplete that does not work on only one view. The reason it does not work is that instead of json, it makes jsonp request (by sniffing I saw that it calls passed url with ?callback=jQueryxxxxxxxxx
), and success function is never called because jquery packs it into anonymous function whose name is passed in callback argument, and server returns standard json (I don't want to use jsonp as it is POST request and NOT cross-domain request). I checked, both current view url and this u
for ajax url argument are on http://localhost:8080/myapp/areax/...
, so I don't see why jQuery makes JSONP request here.
它是tb自动完成的一部分,不仅适用于一个视图。它不起作用的原因是它代替json,它使jsonp请求(通过嗅探我看到它调用传递的url与?callback = jQueryxxxxxxxxx),并且永远不会调用success函数,因为jquery将它打包到名称传递的匿名函数中在回调参数中,服务器返回标准的json(我不想使用jsonp,因为它是POST请求而不是跨域请求)。我检查了,当前的视图url和你的ajax url参数都在http:// localhost:8080 / myapp / areax / ...上,所以我不明白为什么jQuery在这里发出JSONP请求。
EDIT:
编辑:
View on which this does not work has url request is made is like this: http://hostname:8080/AreaName/Report/ViewReport and u parameter of ajax is like /AreaName/MyAutoComplete/Search, so complete url to which autocomplete is made is like http://hostname:8080/AreaName/MyAutoComplete/Search?callback=jQuery151013129048690121925_1327065146844
这个不起作用的视图有url请求是这样的:http:// hostname:8080 / AreaName / Report / ViewReport和ajax的参数就像/ AreaName / MyAutoComplete / Search,所以自动完成的完整url是make就像http:// hostname:8080 / AreaName / MyAutoComplete / Search?callback = jQuery151013129048690121925_1327065146844
Server's response looks like this:
服务器的响应如下所示:
[{"Id":2,"Text":"001"},{"Id":7,"Text":"002"}]
I know it is not jsonp, for that it should be
我知道它不是jsonp,因为它应该是
<script>
jQuery151013129048690121925_1327065146844([{"Id":2,"Text":"001"},{"Id":7,"Text":"002"}]);
</script>
But I want to make normal json request, not jsonp.
但我想制作正常的json请求,而不是jsonp。
UPDATE
UPDATE
Weirdest thing of all (I'm starting to think it is a bug in jQUery v1.5.1 which is used on project) is that when I remove dataType: "json"
, it makes a normal json request :)
最奇怪的事情(我开始认为它是项目中使用的jQUery v1.5.1中的一个错误)是当我删除dataType:“json”时,它会产生一个正常的json请求:)
So, instead of how to make json request, now I will accept an explanation to why this works as expected (and the one with dataType:"json" does not):
所以,不是如何制作json请求,现在我将接受一个解释为什么它按预期工作(并且dataType:“json”的那个没有):
$.ajax({
url: u, type: "POST",
data: data,
success: function (d) { response($.map(d, function (o) { return { label: o.Text, value: o.Text, id: o.Id} })); }
});
1 个解决方案
#1
2
From the bug here : http://bugs.jquery.com/ticket/8118
从这里的错误:http://bugs.jquery.com/ticket/8118
You are probably using jquery-validation plugin. Jquery-validation plugin is not compatible with jQuery 1.5 and the conflict causes the kind of issue you are having here.
您可能正在使用jquery-validation插件。 Jquery-validation插件与jQuery 1.5不兼容,冲突导致您遇到的问题。
If the problem is not specifically due to jquery-validation plugin, check if you have any other jquery plugin that might not be compatible with jQuery 1.5
如果问题不是由jquery-validation插件特别引起的,请检查是否有任何其他可能与jQuery 1.5不兼容的jquery插件
#1
2
From the bug here : http://bugs.jquery.com/ticket/8118
从这里的错误:http://bugs.jquery.com/ticket/8118
You are probably using jquery-validation plugin. Jquery-validation plugin is not compatible with jQuery 1.5 and the conflict causes the kind of issue you are having here.
您可能正在使用jquery-validation插件。 Jquery-validation插件与jQuery 1.5不兼容,冲突导致您遇到的问题。
If the problem is not specifically due to jquery-validation plugin, check if you have any other jquery plugin that might not be compatible with jQuery 1.5
如果问题不是由jquery-validation插件特别引起的,请检查是否有任何其他可能与jQuery 1.5不兼容的jquery插件