Bug Tracker

Opened 12 years ago

Closed 12 years ago

Last modified 11 years ago

#8302 closed bug (worksforme)

jQuery 1.5 ajax request trying to parse JSON as javascript

Reported by: [email protected] Owned by: [email protected]
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.

Change History (16)

comment:1 Changed 12 years ago by snover

Owner: set to [email protected]
Status: newpending

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

comment:2 Changed 12 years ago by [email protected]

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"}

comment:3 Changed 12 years ago by anonymous

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

comment:4 Changed 12 years ago by snover

Resolution: worksforme
Status: newclosed

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

comment:5 Changed 12 years ago by jitter

Component: unfiledajax
Priority: undecidedlow

comment:6 Changed 12 years ago by 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

comment:7 in reply to:  6 Changed 12 years ago by anonymous

Replying to 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

comment:8 Changed 12 years ago by [email protected]

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.

comment:9 Changed 12 years ago by anonymous

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.

comment:10 Changed 12 years ago by anonymous

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"

comment:11 Changed 12 years ago by [email protected]

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.

comment:12 in reply to:  11 Changed 12 years ago by anonymous

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 [email protected]:

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.

comment:13 Changed 12 years ago by anonymous

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

comment:14 Changed 12 years ago by [email protected]

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.

comment:16 Changed 12 years ago by marceloraposo

I solved using "text json" and eval with result

success: function(result){

var resultProcess = eval(result); ...

}

Note: See TracTickets for help on using tickets.