如何使用AJAX和JQuery用JSON数据实现PUT调用? [英] How to implement a PUT call with JSON data using AJAX and JQuery?
问题描述
我环顾四周并尝试了许多不同的方法,但似乎无法将实际数据传递给控制器的功能.
I've looked around and tried many different methods, but can't seem to pass actual data to my controller's function.
这是一些代码:
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
和oldEntry
都是对象.
alert
行输出以下内容(为了简洁起见,删除了一些属性):
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}}
当我调试控制器动作("UpdateEntry")时,这两个参数都用TimesheetEntry
类默认参数(0)填充.
When I debug my controller action ("UpdateEntry"), the two parameters are filled with the TimesheetEntry
class default parameters (0).
我可以正确传递吗?
推荐答案
dataType
属性仅在从服务器获取数据时使用.将数据发送到服务器时,应将contentType
设置为application/json
.
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.
这篇关于如何使用AJAX和JQuery用JSON数据实现PUT调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!