javascript - Setting response content type header in JSONP request? -


i have use case make cross domain jsonp request.

$.ajax({   url: "http://anyorigin.com/get/?url=<any_website_url>&callback=?'",   datatype: 'jsonp',   success: function(data) {     console.log(data); }}); 

it working fine noticed chinese websites data comes garbled. debugged , found out response-header set to:

response header: content-type:text/javascript; charset=iso-8859-1 

now charset iso-8859-1 creating problem. should have been utf-8. want override charset utf-8. know can using ajax. tried using following code -

$.ajax({   url: "http://anyorigin.com/get/?url=www.google.com&callback=?'",   datatype: 'jsonp',   beforesend: function(xhr) {     console.log(xhr);     xhr.overridemimetype("text/javascript; charset=utf-8");   },   success: function(data) {     console.log(data); }}); 

but did not fix problem. guessing jsonp request not use xhr object, not work.

can tell me how can achieve or if achievable? tia.

you correct jsonp not use xhr object, uses script tag.

however, can done through jquery jsonp wrapper using scriptcharset option.

excerpt jquery.ajax docs:

scriptcharset

type: string

only applies when "script" transport used (e.g., cross-domain requests "jsonp" or "script" datatype , "get" type). sets charset attribute on script tag used in request. used when character set on local page not same 1 on remote script.

all have jquery add utf-8 charset attribute jsonp script tag add scriptcharset: 'utf-8' ajax settings object.

example code:

$.ajax({   url: "http://anyorigin.com/get/?url=<any_website_url>&callback=?'",   datatype: 'jsonp',   scriptcharset: 'utf-8',   success: function(data) {     console.log(data); }}); 

Comments