Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.ajax returns jsonp instead of json

I have a simple ajax call like this:

$.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.

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

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

<script>
 jQuery151013129048690121925_1327065146844([{"Id":2,"Text":"001"},{"Id":7,"Text":"002"}]);
</script>

But I want to make normal json request, not jsonp.

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 :)

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):

$.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} })); }
});
like image 771
Goran Obradovic Avatar asked Jan 20 '12 12:01

Goran Obradovic


People also ask

What is the difference between JSONP and JSON?

JSONP is essentially, JSON with extra code, like a function call wrapped around the data. It allows the data to be acted on during parsing.

What is JSONP in Ajax?

JSONP stands for JSON with Padding. Requesting a file from another domain can cause problems, due to cross-domain policy. Requesting an external script from another domain does not have this problem. JSONP uses this advantage, and request files using the script tag instead of the XMLHttpRequest object.

Can JSONP be used with Ajax?

JSONP has nothing to do with Ajax, since it does not use XMLHttpRequest. Instead, it dynamically inserts <script> tag into a webpage.

Why is JSONP insecure?

JSONP is vulnerable to the data source replacing the innocuous function call with malicious code, which is why it has been superseded by cross-origin resource sharing (available since 2009) in modern applications.


1 Answers

From the bug here : 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.

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

like image 85
Lucky Murari Avatar answered Sep 28 '22 08:09

Lucky Murari