5

for XMLHttpRequest it is pretty easy:

var xhr = new XMLHttpRequest();
xhr.open('POST', MyUrl, true);
xhr.setRequestHeader('Content-Type', 'application/json')

But how do I achieve this for XDomainRequest?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
pencilCake
  • 51,323
  • 85
  • 226
  • 363
  • I dont think you can do that.. refer http://stackoverflow.com/questions/2657180/setting-headers-in-xdomainrequest-or-activexobjectmicrosoft-xmlhttp – Engineer Feb 27 '13 at 13:34
  • So you suggest that XDomainRequest always sends RAW data and no way to define the data-type I send? – pencilCake Feb 27 '13 at 13:36
  • That is correct. XDomainRequest is very limited, you can learn more about these limitations here: http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx IE10 will have full support for CORS. – monsur Feb 27 '13 at 14:06
  • @SankalpMishra I think this question is ont the same as the one you have mentioned, in which the poster is trying to add a custom http header, while in this post, the poster is trying to change the value of a http header, like `Content-Type` – ZhaoGang Nov 11 '16 at 09:05

1 Answers1

0

It's not possible to override the XMLHttpRequest content-type. You should set it correctly on the server e.g. for php:

header('Content-type: application/json');

If this is not possible, then you can create a local proxy script which mirrors your crossdomain script with the correct header

Kim T
  • 5,770
  • 1
  • 52
  • 79