How to implement a PUT call with JSON data using AJAX and JQuery?

后端 未结 3 1789
情书的邮戳
情书的邮戳 2021-02-07 08:31

I\'ve looked around and tried many different methods, but can\'t seem to pass actual data to my controller\'s function.

Here is some code:

        var UR         


        
相关标签:
3条回答
  • 2021-02-07 09:14
    $.ajax({
            url: window.serverUrl + 'student/event/' + eventId,
            type: 'put',
            data: JSON.stringify(data),
            headers: {
                'x-auth-token': localStorage.accessToken,
                "Content-Type": "application/json"
            },
            dataType: 'json'
    })
    

    This worked for me

    0 讨论(0)
  • 2021-02-07 09:14

    Use headers: {"X-HTTP-Method-Override": "PUT"} and override the POST request type. It works on my project...

    $.ajax({
        type: 'POST', // Use POST with X-HTTP-Method-Override or a straight PUT if appropriate.
        dataType: 'json', // Set datatype - affects Accept header
        url: "http://example.com/people/1", // A valid URL
        headers: {"X-HTTP-Method-Override": "PUT"}, // X-HTTP-Method-Override set to PUT.
        data: '{"name": "Dave"}' // Some data e.g. Valid JSON as a string
    });
    
    0 讨论(0)
  • 2021-02-07 09:18

    The dataType attribute is only used when you're getting data from the server. You should be setting contentType to application/json when sending data to the server.

    0 讨论(0)
提交回复
热议问题