Bug Tracker

Modify

Ticket #8302 (closed bug: worksforme)

Opened 3 years ago

Last modified 2 years ago

jQuery 1.5 ajax request trying to parse JSON as javascript

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

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

comment:1 Changed 3 years ago by snover

  • Owner set to antonio.mertens@…
  • Status changed from new to pending

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

comment:2 Changed 3 years ago by antonio.mertens@…

  • Status changed from pending to new

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 3 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 3 years ago by snover

  • Status changed from new to closed
  • Resolution set to worksforme

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

comment:5 Changed 3 years ago by jitter

  • Priority changed from undecided to low
  • Component changed from unfiled to ajax

comment:6 follow-up: ↓ 7 Changed 3 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 3 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 3 years ago by grahamoneale@…

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 3 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 3 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 follow-up: ↓ 12 Changed 3 years ago by 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.

comment:12 in reply to: ↑ 11 Changed 3 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 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.

comment:13 Changed 3 years ago by anonymous

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

comment:14 Changed 3 years ago by peter.i.ehrlich@…

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:15 Changed 3 years ago by peter.i.ehrlich@…

or maybe this has already been solved. Whoops.

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

comment:16 Changed 3 years ago by marceloraposo

I solved using "text json" and eval with result

success: function(result){

var resultProcess = eval(result); ...

}

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.