如何使用AJAX和JQuery实现带有JSON数据的PUT调用?

时间:2022-10-08 08:19:28

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 URL = "/Timesheet/Timesheet/UpdateEntry";

        var dataObject = { 'newWeekEntry': newEntry, 'oldWeekEntry': oldEntry };

        alert(JSON.stringify(dataObject));

        $.ajax({
            url: URL,
            type: 'PUT',    
            data: JSON.stringify(dataObject),
            dataType: 'json',
            success: function(result) {
                alert("success?");
            }
        });

newEntry and oldEntry are both objects.

newEntry和oldEntry都是对象。

The alert line outputs this (with some properties removed, just for brevity):

警报线输出此信息(删除了一些属性,仅为简洁起见):

{"newWeekEntry":{"MondayHours":2,"TuesdayHours":2,"WednesdayHours":5,"ThursdayHours":5,"FridayHours":"4","SaturdayHours":0,"SundayHours":0},"oldWeekEntry":{"MondayHours":2,"TuesdayHours":2,"WednesdayHours":5,"ThursdayHours":5,"FridayHours":2,"SaturdayHours":0,"SundayHours":0}}

When I debug my controller action ("UpdateEntry"), the two parameters are filled with the TimesheetEntry class default parameters (0).

当我调试我的控制器动作(“UpdateEntry”)时,这两个参数用TimesheetEntry类的默认参数(0)填充。

Am I passing this in properly?

我是否正确地传递了这个?

3 个解决方案

#1


28  

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.

dataType属性仅在从服务器获取数据时使用。在将数据发送到服务器时,您应该将contentType设置为application / json。

#2


3  

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

使用标题:{“X-HTTP-Method-Override”:“PUT”}并覆盖POST请求类型。它适用于我的项目......

$.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
});

#3


1  

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

这对我有用

#1


28  

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.

dataType属性仅在从服务器获取数据时使用。在将数据发送到服务器时,您应该将contentType设置为application / json。

#2


3  

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

使用标题:{“X-HTTP-Method-Override”:“PUT”}并覆盖POST请求类型。它适用于我的项目......

$.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
});

#3


1  

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

这对我有用