简体   繁体   English

jQuery.ajax返回jsonp而不是json

[英]jQuery.ajax returns jsonp instead of json

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. 它是tb自动完成的一部分,不仅适用于一个视图。 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). 它不起作用的原因是它代替json,它使jsonp请求(通过嗅探我看到它调用传递的url与?callback=jQueryxxxxxxxxx ),并且永远不会调用success函数,因为jquery将它打包到名称传递的匿名函数中在回调参数中,服务器返回标准json(我不想使用jsonp,因为它是POST请求而不是跨域请求)。 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. 我检查了,当前的视图url和这个u的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} })); }
});

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插件。 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 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插件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM