Unexpected token on jsonp request

16 views
Skip to first unread message

Toni Thomä

unread,
Jul 29, 2011, 2:16:37 PM7/29/11
to us...@couchdb.apache.org
hello,
i´m firing an ajax/jsonp reqeust to my cloudant app:

var obj = $.ajax({
url: "http://xyz",
dataType: 'jsonp',
success: function(data) {
//SOME CODE
},
error: function() {
//SOME CODE
}
});

most of the time the response is ok and i can read out my data.
but sometimes I´m getting the following error (with the same query string that already worked):

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

i need to make this request an jsonp request, AFAIK jsonp returns as a script and gets executed by the browser.
do i have to set a request header?
(I´m using GoogleChrome)

cheers,
toni

Max Ogden

unread,
Jul 29, 2011, 2:21:56 PM7/29/11
to us...@couchdb.apache.org
in your configuration pane from futon make sure that allow_jsonp is set to
true

here is an example NON jsonp response, e.g. http://couch/database/123:

{"pizza":"cats"}

here is the same resource with jsonp enabled, e.g.
http://couch/database/123?callback=taco:

taco({"pizza":"cats"})

as you can see, to test if jsonp is enabled you just have to add a callback
parameter to your request and you should see your data wrapped in a function
with the same name as the parameter

Toni Thomä

unread,
Jul 30, 2011, 4:07:53 AM7/30/11
to us...@couchdb.apache.org
Jsonp is enabled per default on cloudant.
the response i´m getting is correct most of the time.
the syntax error "only" happens on 10% of the requests.

seems like javascript cannot parse the jsonp response in some cases because the response is sent with "text/plain" but js tries to interpret the response as a script.

cheers,
toni

Reply all
Reply to author
Forward
0 new messages