Syntax error in a valid JSON document

I am using Node js and this is how I build my response :

response.writeHead(200, {"Content-Type": "script"});
response.write(JSON.stringify({"test":"fail"}));
response.end();

The client makes a jsonp request and in Chrome as in Safari I get this error :

Resource interpreted as Script but transferred with MIME type text/plain.
SyntaxError: Unexpected token ':'

The browser gives access to the response it recieved :

{"test":"fail"}

Where does the syntax error come from ?

Answers


The client is trying to execute the Script resource (it is interpreted as Script, not JSON). {"test":fail"} is invalid JavaScript -- try running it in the console.

jsonp requires a callback that is ordinarily requested by the client so that it can be run properly. You should actually be doing something like:

response.write(request.query.callback + "(" + JSON.stringify(json) + ")");

This will be emitted as

callback12345({"test":"fail"})

which is valid JavaScript and will run properly.


Need Your Help

NullpointerException when I try to set a EditText with information from another class

android android-asynctask gps android-edittext

i´ve read a lot of post that had almost the same question but it doesn´t help me with my problem, the thing is that I am trying to create an app to constantly update the information in 2 EditTexts ...

Comparing Dates in different Datatypes in MYSQL

mysql database date

Facing a weird issue.I have stored a expiry date in my database as Varchar(Due to a calender plugin).I have to now check for the expired items in the table.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.