Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery POST request actually sends as GET

I'm trying to use the following code to send a POST request:

$.ajax({
    type: "post",
    url: 'http://api.com/'+apiUsername+'/'+apiBucket+'/elements/add',
    dataType: 'jsonp',
    contentType: "application/json",
    data: JSON.stringify({
        username: apiUsername,
        api_key: APIkey,
        elementPermalink: tURL
    }),
    success: function() {
        console.log('posted!');
    }
});

However, this always goes through as a GET request, not a POST request, and the API server consequently rejects it. Why is jQuery insisting on making this a GET request?

(This is intentionally cross-domain, but it's JSONP so that's not a problem.)

like image 255
futuraprime Avatar asked Oct 11 '11 12:10

futuraprime


1 Answers

JSONP is a GET only so dataType: 'jsonp', will always be a get

Think of JSONP like this:

<script src="http://url.com/?query=string"></script>

Since that's how jsonp gets around cross-domain, it can only be a get request.

like image 92
Joe Avatar answered Sep 28 '22 00:09

Joe