jQuery POST request actually sends as GET

我只是一个虾纸丫 提交于 2019-12-23 09:58:44

问题


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.)


回答1:


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.



来源:https://stackoverflow.com/questions/7725929/jquery-post-request-actually-sends-as-get

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!