gpt4 book ai didi

jquery - 无效的 JSON 类型 MVC 3

转载 作者:行者123 更新时间:2023-12-01 06:54:32 25 4
gpt4 key购买 nike

我在整个网络上进行了搜索,我认为我正确使用了 JSON,但我只是找不到它的问题所在。我需要将此 json 对象作为 MVC Controller 操作的参数传递:

JSON:

{ 
"ContactId": "0",
"Fax2": "dsad",
"Phone2": "dsad",
"Tittle": "asdsa",
"Cellphone": "dsadsd",
"Address": { "City": "kjshksdfks",
"Country": "undefined",
"Id": "0",
"State": "dsadsa"}
}

.NET 对象如下:

public class Contact
{
public string ContactId {get; set;}
public string Fax2 {get; set;}
public string Phone2 {get; set;}
public string Tittle {get; set;}
public string Cellphone {get; set;}
public Address ContactAddress {get; set;}
}

嵌套地址类型如下:

public class Address
{
public string City {get; set;}
public string Country {get; set;}
public int Id {get; set;}
public string State {get; set;}
}

我的 JSON 有什么问题吗?为了将 clean 传递给 Controller ​​操作,我缺少什么

确切的错误是:无效的 JSON 原语:联系方式

注意:我尝试使用 javascript 对象、JSON.stringify 和 toJSON()

提前致谢!

最佳答案

我怀疑您缺少 ajax 调用的内容类型。另请参阅:http://forums.asp.net/t/1665485.aspx/1?asp+net+mvc+3+json+values+not+recieving+at+controller .

我更改了一些内容 1) 标题 -> 标题和地址 -> 联系地址;这应该有效:

JSON:

{
"ContactId": "a",
"Fax2": "b",
"Phone2": "c",
"Title": "d",
"Cellphone": "e",
"ContactAddress": {
"Id": 22,
"City": "a",
"State": "b",
"Country": "c"
}
}

调用:

<script>
var contact = { "ContactId": "a", "Fax2": "b", "Phone2": "c", "Title": "d", "Cellphone": "e", "ContactAddress": { "Id": 22, "City": "a", "State": "b", "Country": "c"} };

$.ajax({
type: 'POST',
url: '[YOUR-URL]',
data: JSON.stringify(contact),
success: function (result) {
alert('success');
},
dataType: "json",
contentType: "application/json; charset=utf-8"
});
</script>

关于jquery - 无效的 JSON 类型 MVC 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10577103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com