我有以下设置
在客户端,我正在使用像这样的复杂Javascript变量
var order = {
name:"",
id:"",
cost:"",
details:{
sItem:[{name:"",cost:""}],
dItem:[{name:"",cost:"", components:[{name:"",quantity:""}]}]
}
}
我在c#服务器上有一个像这样的
控制器 public string getCost(string order)
{
var sOrder = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(order);
//do the processing here
return "I am processed";
}
在我的Javascript中,我称之为
$.getJSON("Api/menu/getCost/" + JSON.stringify(order),
function (data) {
window.alert('i m here : success');
});
问题是,当我发送此请求时,服务器响应错误的请求,但是如果我附加一个简单的字符串,如“hello”而不是JSON.stringify(order)。控制器接收它并成功返回没有问题,所以我知道问题是在将订单转换为JSON但不知道如何查找的地方。
是的,我正在使用getJSON并返回一个简单的字符串,但这不是问题,因为我将在处理完成后返回一个JSON字符串。实际上,这将是收到的相同JSON,其中一些属性值已更改。
答案 0 :(得分:0)
我玩$ .get,$。postt,$ .getJSON等。虽然我让其中一些工作,但他们总是返回jqXHR,即使响应是一个序列化的JSON对象。 以下是我成功的唯一方法。 (请注意,我正在访问网络服务。)
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: "WebService1.asmx/GetAList",
// data: JSON.stringify({ s: parms }),
type: 'post',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data, status) {
var contries = JSON.parse(data.d);
$("#sel2").select2({ data: contries });
},
error: function (one, two) {
debugger;
}
});
});
</script>
</head>
<body>
<form>
<div>
<p>populated from ajax</p>
<select style="width:175px" id="sel2"></select>
</div>
</form>
</body>
</html>
和我的网络服务
使用System.Web.Script.Services;
namespace WebApplication1
{ // a class in context with how the data are is being used
[Serializable]
public class select2Item
{
public String id { get; set; }
public String text { get; set; }
public select2Item(String code, String name)
{
this.id = code;
this.text = name;
}
public select2Item() { }
}
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string GetAList()
{
System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
return makelist();
}
// return a list of serialized codes and countries
public String makelist()
{
List<select2Item> list = new List<select2Item>();
list.Add(new select2Item("AI", "Anguilla"));
list.Add(new select2Item("AQ", "Antarctica"));
list.Add(new select2Item("AG", "Antigua and Barbuda"));
list.Add(new select2Item("AR", "Argentina"));
list.Add(new select2Item("AM", "Armenia"));
list.Add(new select2Item("AW", "Aruba"));
list.Add(new select2Item("AU", "Australia"));
list.Add(new select2Item("AT", "Austria"));
list.Add(new select2Item("AZ", "Azerbaijan"));
list.Add(new select2Item("BS", "Bahamas"));
list.Add(new select2Item("BH", "Bahrain"));
list.Add(new select2Item("BD", "Bangladesh"));
list.Add(new select2Item("BB", "Barbados"));
list.Add(new select2Item("BY", "Belarus"));
list.Add(new select2Item("BE", "Belgium"));
list.Add(new select2Item("BZ", "Belize"));
// did it this way to show you which to use
System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
String jsonList = ser.Serialize(list);
return jsonList;
}
}
}