views:

132

answers:

1

I need to make a jsonp POST request with the content type 'application/json'. I can get the POST request to the server like this:

      jQuery.ajax({
        type: 'POST',
        url: url,
        data: data,
        success: success,
        error: error,
        async: true,
        complete: complete,
        timeout: TIMEOUT,
        scriptCharset: 'UTF-8',
        dataType: 'jsonp',
        jsonp: '_jsonp',
      });

But as soon as I add the line:contentType: "application/json" it starts sending it as an OPTIONS request rather than a POST.

How can I specify the content type and still submit the request as a POST?

+2  A: 

It is not possible to make a JSONP POST request.

JSONP works by creating a <script> tag that executes Javascript from a different domain; it is not possible to send a POST request using a <script> tag.

SLaks
If I leave out the contentType, the request is made and the server gets it as a POST.
Marcus
Yeah, jQuery falls back to XMLHttpRequest as you can't POST via `<script>` inclusion. But that means the JSONP response will never work; if it's coming from a different hostname you won't be able to read anything from the response due to the Same Origin Policy. `<script>` inclusion can only trigger a `GET`, any data you want to send to it must go in URL query parameters.
bobince
Makes sense, thanks for the explanation.
Marcus