数据JSON.stringify()或默认的URL编码贴通过ajax? [英] JSON.stringify() or default url encoding for data posted via ajax?

查看:446
本文介绍了数据JSON.stringify()或默认的URL编码贴通过ajax?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发布使用复杂对象的不同或优势是什么 JSON.stringify()通过简单地允许数据的默认url编码,使用<$ C时, $ C> jQuery.ajax 的功能呢? 在MVC的WebAPI我张贴,以能够正确地路由和解析这两个要求,所以不存在'的明确的需要JSON发送到服务器。 此外,由于CORS是在服务器上启用,张贴JSON发出preflight OPTIONS请求(我明白为什么......这不是我的问题),而使用的默认编码不对,从而节省了一个网络电话这将' 看起来的从性能的角度来看是有利的。

这两个AJAX调用工作。我已经与一个对象,还包含嵌入式子对象和一个嵌入式阵列测试

  VAR项目= {
    产品名称:乔,
    团队:美国佬,
    关键参数:{年龄:21,身高:6'0},
    当前位置:[第一,右,卫生署]};

//使用默认URL编码
$。员额(apiUri,项)
.done(函数(){警报(完成);})
.fail(函数(){警报(失败);});

//张贴JSON字符串
$阿贾克斯({
    网址:apiUri,
    的contentType:应用/ JSON
    数据:JSON.stringify(项目),
    键入:POST
})
.done(函数(){警报(完成);})
.fail(函数(){警报(失败);});
 

然而,许多AJAX到网络API的例子似乎倾向于发布数据作为JSON字符串。

指南或建议?

解决方案
  

使用JSON.stringify()在使用jQuery.ajax功能数据的默认url编码张贴JSON的差异和优势是什么?

这是真的不太具有可比性,你错过的内容类型。换句话说发布JSON作为X WWW的形式,urlen codeD简单不起作用。所以,我要调整你的问题更具体:

  

使用JSON.stringify()设置为应用程序/ JSON 在默认的URL编码的内容类型(<$ C $张贴JSON的差异和优势是什么C> X WWW的形式,urlen codeD /查询字符串使用jQuery.ajax函数的数据)?

在我看来,(但还是事实),最大的和最有利的优势是,当你使用JSON,而不是一个表格查询字符串,工作正确使用值绑定到一个类所需的量默认的模型粘合剂是最小的。假设网络API使用相同的模型绑定的MVC(我pretty的肯定它)绑定形式值可以的相当繁琐和难以调试。这往往张贴简单的名称值对的时候不要出现这种情况。

What is the difference or advantage of posting a complex object using JSON.stringify() over simply allowing the default url encoding of the data, when using the jQuery.ajax functions? The MVC WebApi I am posting to is able to correctly route and parse both requests, so there is no 'explicit' need to send JSON to the server. Additionally, because CORS is enabled on the server, posting json emits a preflight OPTIONS request (I understand why...that is not my question), whereas using the default encoding does not, thus saving one network call which would 'seem' to be advantageous from a performance standpoint.

Both of these ajax calls work. I've tested with an object that also contains an embedded sub-object and an embedded array:

var item = {
    Name: "Joe", 
    Team: "Yankees", 
    Vitals: { Age: 21, Height: "6'0" }, 
    Positions: ["1st", "Right", "DH"]};

// using default url-encoding
$.post(apiUri, item)
.done(function () {alert("Done");})
.fail(function () {alert("Fail");});

// posting a json string
$.ajax({
    url: apiUri,
    contentType: "application/json",
    data: JSON.stringify(item),
    type: "POST"
})
.done(function () {alert("Done");})
.fail(function () {alert("Fail");});

However, many ajax to web-api examples seem to favor posting data as a json string.

Guidelines or advice?

解决方案

What is the difference or advantage of posting JSON using JSON.stringify() over the default url encoding of the data using the jQuery.ajax functions?

This is really not quite comparable as you're missing the content type. In other words Posting JSON as x-www-form-urlencoded simple doesn't work. So I'll adjust your question to be more specific:

What is the difference or advantage of posting JSON using JSON.stringify() with the content type set to application/json over the default url encoding (x-www-form-urlencoded/Querystring) of the data using the jQuery.ajax functions?

In my opinion (but still factual), the biggest and most beneficial advantage is that when you use JSON instead of a Form Querystring, the amount of work required to correctly bind the values to a class using the default model binder is minimal. Assuming the web-api uses the same model binder as MVC (I'm pretty sure it does) bind form values can be quite tedious and difficult to debug. This tends not to be the case when posting simple name value pairs.

这篇关于数据JSON.stringify()或默认的URL编码贴通过ajax?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆