P.B*_*key 2 javascript ajax json asp.net-mvc-3
我在其他SO帖子中读到"javascript is JSON".我很难将这个理论翻译成我的应用程序.我用jQuery执行POST
$.ajax({
type: 'POST',
url: 'Pricing/Create',
data: items,
success: function () { alert('successfully called pricing'); },
dataType: 'json'
});
Run Code Online (Sandbox Code Playgroud)
帖子在我PricingController的Create方法中成功击中了断点.在审查我的时候Request.QueryString,它是空的.
items是的阵列SomeItem与length = 30.定义为
function SomeItem(description, finalPrice, percentDiscount) {
this.Description = description;
this.FinalPrice = finalPrice;
this.PercentDiscount = percentDiscount;
}
Run Code Online (Sandbox Code Playgroud)
我没有执行JSON转换,因为"javascript是JSON".如何在定价控制器中获取我的数据?
JSON.stringify(items)运行时,我在alert()中看到一组很好的垃圾(在Firebug中也很漂亮):
[{"Description":"some item","Data2":"$1.00","data3":"10"},//...
但是,当它到达服务器...在C#中Request.Form它看起来像:
%5b%7b%22Description%22%3a%22some+item%22%2c%22data2%22 wtflip是......
JSON是'JavaScript Object Notation',而不是JavaScript.您使用JSON来表示JavaScript对象,尤其是当您要将其发送回服务器时.
您需要将JavaScript对象转换为JSON,然后再将其传递给ajax调用 - 这应该可以解决问题:
var json = $.toJSON(items);
Run Code Online (Sandbox Code Playgroud)
阅读本文,它可能会有所帮助:http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx
| 归档时间: |
|
| 查看次数: |
214 次 |
| 最近记录: |