3

CORS works perfectly in my application right now for Chrome, Firefox. Using this plugin https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest

I was able to get CORS requests working in IE also like so:

$.ajax({
    url: url,
    type:"POST",
    dataType: "json"
});

However when I try to send data in the POST request, such as:

$.ajax({
    url: url,
    type:"POST",
    dataType: "json",
    data: {test: 5}
});

It does not work. Has anyone managed to get IE to make CORS requests that have POST data in them?

Thanks!

xd44
  • 831
  • 3
  • 9
  • 15
  • not familiar with CORS, but i believe POST only accepts strings as data. Have you tried putting that 5 in parenthises? – Pevara Aug 19 '12 at 00:58
  • Peter, thanks for your response. The syntax is fine, since is works in modern browsers. This seems to be a problem with IE/ajaxTransport plugin – xd44 Aug 19 '12 at 01:02
  • It's most likely cross domain issue. IE does not allow cross domain default. You could change the ie settings to allow cross domains, but AjaxP is the best way to since you can't change for user browser settings. – Ram G Aug 19 '12 at 01:08
  • The fact that it works fine in modern browsers does not necessarily mean the syntax is fine. IE is known for beeing rather picky on the syntax (for example adding a commaafter the last element in object definition will not work in IE, but will in other browsers) Also i just noticed you are missing a comma after the dataType: "json"... – Pevara Aug 19 '12 at 01:10
  • @Ram IE cross domain is working, just not with POST data – xd44 Aug 19 '12 at 01:16
  • @Peter: The missing comma was a typo when porting code to this page, sorry. Fixed it in the original post. What's the syntax you are suggesting? I think the problem is actually that the jquery plugin makes no attempt to deal with the "data" attribute of the parameters. I'm not sure if it's unimplmented or whether jQuery provides the functionality for ajaxTransports – xd44 Aug 19 '12 at 01:18
  • sorry, i meant quotes, not parentheses (english at this hour :-s) Try changing {test: 5} to {test: '5'} – Pevara Aug 19 '12 at 01:21
  • @PeterVR tried adding single quotes, didn't help :/ – xd44 Aug 19 '12 at 01:31
  • @sogeek crossDomain parameter didn't make a difference. It seems that the request makes it to my server if i don't include the "data" parameters. When I do, it doesn't transmit anything – xd44 Aug 19 '12 at 01:57
  • then, can you post the server side script? – code-jaff Aug 19 '12 at 02:13
  • It's a simple NodeJS Express app, I'm outputting req.body – xd44 Aug 19 '12 at 02:39

1 Answers1

0

I noticed in the jQuery-ajaxTransport-XDomainRequest source, the send function call which passed userOptions.data was commented out and the active send() call has no parameters:

I was running into the same issue with POST and no data getting to the server. So I uncommented the send with data call as so:

xdr.send(userOptions.data);
//xdr.send();

However it still did not send data. So I changed the type from 'POST' to 'GET and updated my server code to handle it. Well it actually worked! I have not done more testing and I'm not sure if GET will be acceptable for the code I'm developing.

UPDATE: I tested further and using send() or send(userOptions.data) seems to make no difference on IE. It was the change to GET that made it work so POST is still an issue if you must use POST in your ajax code.

Since I'm new to cross domain ajax posting of data (IE being the problem), I'm hoping others will post their findings so it works for POST as well as GET. Thanks for all your help!

Meliovation
  • 350
  • 3
  • 12