• 0

JQuery, XmlHttpRequest, the OPTIONS verb and Mr. Preflight


Question

Even confident developers get stuck sometimes!

I've recently been doing some Cross-domain javascript using JSONP, and ASP.NET MVC.

The particular Controller action will only respond to a POST request, this is by design.

In IE8, I can see (via Fiddler2) that the response is correct, and returning a HTTP 200 response, along with the JSONP javascript.

In Firefox, Safari and Chrome, the response is still being returned, with the appropriate HTTP 200 code and JSONP content, the only difference is that the XmlHttpRequest object being used by JQuery is setting the status code to 0, and the responseText to empty.

Originally, I thought this was due to COR HTTP Preflighting (Http Access Control), whereby a custom header or a content-type other than text/plain would cause an additional HTTP request (with an OPTIONS) verb to be sent to the server. I can see in Fiddler2 that the OPTIONS request is being responded to with a HTTP 404.

The web server is IIS7 (but the production web server will be an IIS6 box). In IIS7, I can see the standard OPTIONSVerbHandler listed in the handlers, but I'm not convinced this is actually doing anything (in fact, I can't even find any documentation about the OPTIONSVerbHandler anywhere).

To get round this, I modifed the JQuery library to not set the custom header, and change the content-type to text/plain instead of application/json, and Firefox finally starts bypassing the OPTIONS request, and just plain POSTs.

The problem still lies in an empty response (according to the XmlHttpRequest object), even though Fiddler2 shows that a successful HTTP 200 response, with content is being returned.

Any help?

7 answers to this question

Recommended Posts

  • 0

It appears to be part of the design of the jQuery library. Checking through the source (v1.3.2), it only does a JSONP callback via the script tag with the Http type set to GET (which actually makes sense), switching to a GET instead of POST resolves the issue.

  • 0
It appears to be part of the design of the jQuery library. Checking through the source (v1.3.2), it only does a JSONP callback via the script tag with the Http type set to GET (which actually makes sense), switching to a GET instead of POST resolves the issue.

Good to know. I'll jot that one down. :)

  • 0
Even confident developers get stuck sometimes!

I've recently been doing some Cross-domain javascript using JSONP, and ASP.NET MVC.

The particular Controller action will only respond to a POST request, this is by design.

In IE8, I can see (via Fiddler2) that the response is correct, and returning a HTTP 200 response, along with the JSONP javascript.

In Firefox, Safari and Chrome, the response is still being returned, with the appropriate HTTP 200 code and JSONP content, the only difference is that the XmlHttpRequest object being used by JQuery is setting the status code to 0, and the responseText to empty.

Originally, I thought this was due to COR HTTP Preflighting (Http Access Control), whereby a custom header or a content-type other than text/plain would cause an additional HTTP request (with an OPTIONS) verb to be sent to the server. I can see in Fiddler2 that the OPTIONS request is being responded to with a HTTP 404.

The web server is IIS7 (but the production web server will be an IIS6 box). In IIS7, I can see the standard OPTIONSVerbHandler listed in the handlers, but I'm not convinced this is actually doing anything (in fact, I can't even find any documentation about the OPTIONSVerbHandler anywhere).

To get round this, I modifed the JQuery library to not set the custom header, and change the content-type to text/plain instead of application/json, and Firefox finally starts bypassing the OPTIONS request, and just plain POSTs.

The problem still lies in an empty response (according to the XmlHttpRequest object), even though Fiddler2 shows that a successful HTTP 200 response, with content is being returned.

Any help?

Thread bump!

Would you mind showing me how you modified jquery to not send the OPTIONS verb in Firefox? I have the same problem as you did. Either that, or getting IIS to understand the OPTIONS verb

  • 0

Hi,

The modification was actually not required. When you make a JSONP call, it actually achieves the cross-domain transparency by creating a new SCRIPT element on the page. Because you can't make a POST call from a SCRIPT element (it's only ever a GET), simply changing your POST to a GET will stop Firefox sending the OPTIONS header ahead for validation.

  • 0
Hi,

The modification was actually not required. When you make a JSONP call, it actually achieves the cross-domain transparency by creating a new SCRIPT element on the page. Because you can't make a POST call from a SCRIPT element (it's only ever a GET), simply changing your POST to a GET will stop Firefox sending the OPTIONS header ahead for validation.

So you're saying a cross domain POST is impossible using jQuery? My issue is that using a GET, I have to put the parameters in the querystring which I didn't want to do as that stuff will not be encrypted. (I was planning on POST'ing to an HTTPS site with authentication info). While developing I hosted the webservice on the same site and I was able to POST to it using $.post and sending the result to a callback. Now that it's been tested I moved the webservice over to HTTPS and herein lies the problem.

  • 0
So you're saying a cross domain POST is impossible using jQuery? My issue is that using a GET, I have to put the parameters in the querystring which I didn't want to do as that stuff will not be encrypted. (I was planning on POST'ing to an HTTPS site with authentication info). While developing I hosted the webservice on the same site and I was able to POST to it using $.post and sending the result to a callback. Now that it's been tested I moved the webservice over to HTTPS and herein lies the problem.

There is the problem though. You can't do a POST from a SCRIPT element. The browser will see the url that is set as it's source, and do a GET request on that, just like it would in any other resource (such as other SCRIPTS and LINKS [stylesheets]). The thing which will confuse many people, is that because they are actually doing the JSONP action via JQuery's ajax call, they assume its being done via XmlHttpRequest. It's not actually doing this, it's simply telling the browser there is another script to load. The way JSONP works, is that you pass a callback function name to whatever service you are dynamically calling, and that service has to wrap the JSON serialised data in that function call which allows it to evaluated at the client browser, e.g.:

GET http://somedomain.com/someservice/getMeSom...back=function01

Which should return it's serialised data something akin to:

function01({ data: { name = "Test", age = 25 }});

The browser succesfully returns that data because its a GET request across domains (which is allowed), and executes that function 'function01'.

Now, with the JSONP datatype, jquery automatically generates that callback function and name (this is overridable) and transparent handles this for you.

Unforetunately, you can't do this via POST. Hope that clears up the confusion somewhat.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • All it does is use the CPU more efficiently during boot to speed up boot times. That's it. Yawn....
    • It's not a one or the other kind of thing. Software should run efficiently, and the operating system should appropriately manage the CPU clocks. You could have the best most optimized software on earth, and it will still run faster if the CPU does a better job of boosting as needed. All this is doing is pre-boosting the CPU based on user actions, instead of waiting for the normal detection mechanism to kick in. If the OS knows it is about to need more CPU, why shouldn't it use that knowledge? It's the same idea of downshifting before passing someone, instead of just burying your foot into the peddle and waiting for the transmission to figure out what you want to do.
    • Audacity 3.7.8 by Razvan Serea Audacity is a free, open source digital audio editor and recording application. Edit your sounds using cut, copy, and paste features (with unlimited undo functionality), mix tracks, or apply effects to your recordings. The program also has a built-in amplitude-envelope editor, a customizable spectrogram mode, and a frequency-analysis window for audio-analysis applications. Built-in effects include bass boost, wah wah, and noise removal, and the program also supports VST plug-in effects. You can use Audacity to: Record live audio. Record computer playback on any Windows Vista or later machine. Convert tapes and records into digital recordings or CDs. Edit WAV, AIFF, FLAC, MP2, MP3 or Ogg Vorbis sound files. AC3, M4A/M4R (AAC), WMA and other formats supported using optional libraries. Cut, copy, splice or mix sounds together. Numerous effects including change the speed or pitch of a recording. Write your own plug-in effects with Nyquist. And more! See the complete list of features. Audacity 3.7.8 changelog: #10688 Fixed an exception thrown when pasting into a newly-created track (Thanks, David Bailes (@DavidBailes)!) #10870, #10884, #10775, #10629 Fixed tone generation, waveform-scale setting, SetClip Name parameter, and clip-boundary command names for scripting and macros (Thank you, David Bailes (@DavidBailes)!) #11106 Fixed the loading of presets for the Distortion effect (A million thanks, David Bailes (@DavidBailes)!) #10947 Fixed paste into an empty audio track not preserving the source sample rate (Thanks, Juan Gabriel Colonna (@juancolonna)!) #10776 Allowed AltGr modifier in label and clip name editing (Thanks, Davide Peressoni (@DPDmancul)!) #9938 Added options to choose where silence is truncated (start/middle/end) (Thanks, Noah Rosenfield (@nosenfield)!) #9935 Added Podcast 2.0 chapters JSON export for label tracks (Thanks, Noah Rosenfield (@nosenfield)!) #10103 Improve UI on HiDPI displays on Linux/wxGTK (Thanks, Ivan A. Melnikov (@iv-m)!) #10099 Fixed MixerBoard Mute and Solo button display (Thanks, Ivan A. Melnikov (@iv-m)!) #10681 Fixed multichannel FLAC import #10999 Fixed envelope being broken after joining clips Download: Audacity 64-bit | Standalone ~20.0 MB (Open Source) Download: Audacity 32-bit | Standalone Download: Audacity ARM64 | Standalone View: Audacity Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • There really isn't anything magical about the low latency profile, other OS's do this as well. All they're doing is using your CPUs boost clock options in a more smarter way.
    • So we shouldn't have the option because of people using their laptops on battery? OK? LOL
  • Recent Achievements

    • One Month Later
      highriskpaym earned a badge
      One Month Later
    • Week One Done
      highriskpaym earned a badge
      Week One Done
    • One Year In
      highriskpaym earned a badge
      One Year In
    • Week One Done
      FBSPL earned a badge
      Week One Done
    • One Year In
      Jim Dugan earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      494
    2. 2
      PsYcHoKiLLa
      198
    3. 3
      +Edouard
      155
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      70
  • Tell a friend

    Love Neowin? Tell a friend!