简体   繁体   中英

How can I correctly send data through 'PUT' with ajax in the request body like Backbone does

I noticed that when saving data along with an id in backbone what it does is to send a 'PUT' request along with the data inside the request body ( in chrome dev tools appears as "request payload"). How can I achieve this with JQuery Ajax? I have tried in several ways but none seems to send the data just like Backbone does.

$.ajax({
    url: '/v2/users/request/test',
    type: 'PUT',
    dataType: 'json',
    data: JSON.stringify({ "command": "on" }),
    success: function(data, status, jqXHR){
    },
    error: function(jqXHR, status, errorThrown){
    }
})

in php I have this example on how to retrieve body data and display it:

if ($_SERVER['REQUEST_METHOD'] == 'PUT'){
            return file_get_contents('php://input');
        }

仅通过将内容类型显式添加到application / json即可工作:

contentType: 'application/json',

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM