Skip to main content

Bug Tracker

Side navigation

#8302 closed bug (worksforme)

Opened February 17, 2011 05:43PM UTC

Closed February 17, 2011 06:18PM UTC

Last modified March 14, 2012 11:38AM UTC

jQuery 1.5 ajax request trying to parse JSON as javascript

Reported by: antonio.mertens@gmail.com Owned by: antonio.mertens@gmail.com
Priority: low Milestone: 1.next
Component: ajax Version: 1.5
Keywords: Cc:
Blocked by: Blocking:
Description

if I make a $.ajax request using dataType: "json" and return a json result, I get an error parsing ":".

After some investigation I realized the exception was in line 601 of jQuery file and saw that it was trying to parse json as a script.

If I put in the dataType: "text json", it works and converts the server response to json as it should with just "json".

With version 1.4.4 it works fine with dataType json.

Attachments (0)
Change History (16)

Changed February 17, 2011 05:59PM UTC by snover comment:1

owner: → antonio.mertens@gmail.com
status: newpending

Please provide a test case demonstrating this issue. Also, please paste the server’s JSON response including headers.

Changed February 17, 2011 06:07PM UTC by antonio.mertens@gmail.com comment:2

status: pendingnew

I'm sorry but I can't seem to get fiddler to work with /echo/json so I can't provide an example right now. I've tested on my server and below are the server response.

jQuery Error: "Uncaught SyntaxError: Unexpected token :"

Response Headers:

Cache-Control:private

Content-Length:53

Content-Type:application/json; charset=utf-8

Date:Thu, 17 Feb 2011 18:05:10 GMT

Server:Microsoft-IIS/7.5

X-AspNet-Version:2.0.50727

X-AspNetMvc-Version:2.0

X-Powered-By:ASP.NET

Response Content

{"result":"OK","message":"Criado evento com o ID 85"}

Changed February 17, 2011 06:08PM UTC by anonymous comment:3

On more note: I've found out this was happening because both fullcalendar plugin and jquery.form plugin were giving the same error.

Changed February 17, 2011 06:18PM UTC by snover comment:4

resolution: → worksforme
status: newclosed

I’m not able to reproduce this issue. test case

Changed February 17, 2011 06:51PM UTC by jitter comment:5

component: unfiledajax
priority: undecidedlow

Changed February 28, 2011 01:50AM UTC by anonymous comment:6

Im having the same problem, i get parse error and the only way around this is putting dataType="text json"

I get parse error and jQuery15108338082315804084_1298857654378 was not called

Changed February 28, 2011 01:53AM UTC by anonymous comment:7

Replying to [comment:6 anonymous]:

Im having the same problem, i get parse error and the only way around this is putting dataType="text json" I get parse error and jQuery15108338082315804084_1298857654378 was not called

Nevermind apparently the problem is caused by the validator plugin which is not compatible with version 1.5

Changed March 02, 2011 05:33AM UTC by grahamoneale@gmail.com comment:8

I also receive this error, and I revert back to 1.4.4 and re-test, it works fine. Error is:

XHR finished loading: "http://localhost:8882/forms/ajax/validate-captcha?callback=jQuery151036387112317606807_1299043692429".

jquery-1.5.1.min.js:16Uncaught SyntaxError: Unexpected token :

I have a validate-captcha json test and it returns a boolean, however doesn't appear to work for me in 1.5.1.

Have to revert to 1.4.4 for now.

Changed March 02, 2011 09:55AM UTC by anonymous comment:9

grahamoneale, this ticket is closed. Can you reproduce the bug with fiddle?

I think the problem is with .NET JsonResult.

When I opened the ticket I was using ASP.NET MVC, are you using ASP.NET MVC too?

I believe that the JsonResult returned by microsoft mvc returns something different than what jquery is expecting.

Changed March 03, 2011 12:11AM UTC by anonymous comment:10

I'm getting the same error - previously worked with jquery-1.4.1. When I change the dataType to "text json", I then get the error "No Transport"

Changed March 13, 2011 09:40PM UTC by igor@oleinikov.ru comment:11

I have same problem.

The difference between test case and case of the issue is content type.

Test case:

Content-Type: application/json

Buggy case:

Content-Type: application/json; charset=utf-8

When using dataType 'text json', it works.

Changed March 16, 2011 01:00PM UTC by anonymous comment:12

Hi,

I have the same problem, on IE8 works fine with dataType:"text json" but do not works on FireFox 4RC and Chrome 10 (JSON.parse Error).

I solve it removing some special chars from json data that my server page returns. For example, json data like that:

{"htmlData":"

<table>

<tr>

<td></td>

</tr>

</table>"

}

I have to remove the tabs and spaces of the html format.

Replying to [comment:11 igor@…]:

I have same problem. The difference between test case and case of the issue is content type. Test case: Content-Type: application/json Buggy case: Content-Type: application/json; charset=utf-8 When using dataType 'text json', it works.

Changed March 18, 2011 05:43PM UTC by anonymous comment:13

Using dataType 'text json' seems to have fixed it for me on IE and Chrome.

Changed March 20, 2011 02:08AM UTC by peter.i.ehrlich@gmail.com comment:14

Reproduced. Here's what I can figure out:

Alots changed with how datatypes are handled with the new jQuery. The bug, i think, is evidenced by this:

For an ajax request:

  • jquery 151, datatype: json:

-- Accept:text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01

  • jquery 144, datatype: json:

-- Accept:application/json, text/javascript, */*; q=0.01

Setting the data type to text json gives an identical accept header to simply text, disabling the automatic processing:

  • Accept:text/html, */*; q=0.01

I figure this change isin't intentional, as it breaks the rails respond_with method completely. From what I can tell from the source, dataTypes is an array ['script', 'json'], and only dataTypes[0] is being used to generate the header. The 'script' is added to the array in this line (6651 about):

s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().split( rspacesAjax );

But what's going on there, I can't tell. I tried to do a test, by putting immediately after:

  • s.dataTypes[0] = 'json';

But the array remained logged as: ['script', 'json']

I tried instead

  • s.dataTypes[0] = 'json!';

And it now displayed as one would expect ['json!']

Well, I'm stumped.

Changed March 20, 2011 02:22AM UTC by peter.i.ehrlich@gmail.com comment:15

or maybe this has already been solved. Whoops.

[rails] http://stackoverflow.com/questions/4955582/jquery-1-5-and-new-text-json-datatype

Changed March 25, 2011 06:47PM UTC by marceloraposo comment:16

I solved using "text json" and eval with result

success: function(result){

var resultProcess = eval(result);

...

}