[英]Reading a javascript object as JSON on POST
I read that "javascript is JSON" in other SO posts. 我在其他SO帖子中读到“javascript is JSON”。 I am having difficulty translating this theory to my application.
我很难将这个理论翻译成我的应用程序。 I perform a POST with jQuery
我用jQuery执行POST
$.ajax({
type: 'POST',
url: 'Pricing/Create',
data: items,
success: function () { alert('successfully called pricing'); },
dataType: 'json'
});
The post successfully hits the breakpoint in my PricingController
's Create
method. 帖子在我的
PricingController
的Create
方法中成功点击了断点。 In reviewing my Request.QueryString
, it is empty. 在查看我的
Request.QueryString
,它是空的。
items
is an array of SomeItem
with length = 30
. items
是SomeItem
的数组, length = 30
。 Defined as 定义为
function SomeItem(description, finalPrice, percentDiscount) {
this.Description = description;
this.FinalPrice = finalPrice;
this.PercentDiscount = percentDiscount;
}
I perform no JSON conversion because "javascript is JSON". 我没有执行JSON转换,因为“javascript是JSON”。 How do I get to my data in the Pricing Controller?
如何在定价控制器中获取我的数据?
JSON.stringify(items)
runs I see a nice set of junk in my alert() (also pretty in Firebug):
JSON.stringify(items)
运行时,我在alert()中看到一组很好的垃圾(在Firebug中也很漂亮):
[{"Description":"some item","Data2":"$1.00","data3":"10"},//...
But, when it arrives on the server...in C# Request.Form
it looks like: 但是,当它到达服务器...在C#
Request.Form
它看起来像:
%5b%7b%22Description%22%3a%22some+item%22%2c%22data2%22
wtflip is that... %5b%7b%22Description%22%3a%22some+item%22%2c%22data2%22
wtflip是......
JSON is 'JavaScript Object Notation', not JavaScript. JSON是'JavaScript Object Notation',而不是JavaScript。 You use JSON to represent a JavaScript object, especially when you want to send it back to a server.
您使用JSON来表示JavaScript对象,尤其是当您要将其发送回服务器时。
You do need to convert your JavaScript object to JSON before passing it to the ajax call - this should do the trick: 您需要将JavaScript对象转换为JSON,然后再将其传递给ajax调用 - 这应该可以解决问题:
var json = $.toJSON(items);
Have a read of this, it might help: http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx 阅读本文,它可能会有所帮助: http : //haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx
Have you tried JSON.stringify()
? 你试过
JSON.stringify()
吗?
You can change your data line to: 您可以将数据行更改为:
data: JSON.stringify(items),
If the target browser doesn't natively support JSON.stringify()
, you can Google for a library to fill in that feature. 如果目标浏览器本身不支持
JSON.stringify()
,则可以通过Google为库填写该功能。
Well, if you used the POST method, then it's not going to be in the QueryString variable. 好吧,如果您使用POST方法,那么它不会出现在QueryString变量中。 If you watch what is going on with a fiddler-like tool, is your data (items) being passed back in the body of the request?
如果您使用类似提琴手的工具观察正在发生的事情,您的数据(项目)是否会在请求正文中传回? If so, then you should be able to access it.
如果是这样,那么您应该能够访问它。 Otherwise, something is wrong with your AJAX request.
否则,您的AJAX请求有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.