ajax发布数据是否需要进行URI编码? [英] Does ajax post data need to be URI encoded?

查看:104
本文介绍了ajax发布数据是否需要进行URI编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这一行:

var data = encodeURIComponent(JSON.stringify(object_literal));

我不明白为什么这是URI编码。

I don't understand why this is being URI encoded.

以后的数据将通过 ajax POST 发送。

Later data will be sent via ajax POST.

我知道网址,尤其是您在浏览器地址栏中可以看到的网址,需要使用此处所述的特殊字符:

I understand that URLs, particularly the one you can see in the browser address bar require special characters as described here:

http://www.blooberry.com/indexdot/html/topics/urlencoding.htm

但这究竟与Ajax发布有什么关系?

But what exactly does this have to do with Ajax posting?

同时执行url地址栏内部ajax帖子使用相同的机制?

Do both the url address bar and the internal ajax post utilize the same mechanism?

推荐答案

这一切都取决于内容类型

通常在< form> 使用HTTP方法POST,然后表单值为 URL编码并放置在reqeust的正文中。内容类型标题如下所示:

Normally when a <form> uses the HTTP method POST then the form values are URL Encoded and placed in the body of the reqeust. The content type header looks like this:

content-type: application/x-www-form-urlencoded

大多数AJAX库默认都会这样做,因为它在Web服务器中被普遍接受。但是,没有什么可以阻止您简单地将数据作为JSON或XML进行分类,然后使用不同的内容类型发送它。

Most AJAX libraries will do this by default since it is universally accepted among web servers. However, there is nothing preventing you from simply seralizing the data as JSON or XML and then sending it with a different content type.

content-type: application/json

content-type: text/xml

值得注意的是,有效载荷与AJAX无关!在幕后他们都在使用 XmlHttpRequest 对象,以异步方式向服务器发送HTTP请求。只要您告诉服务器如何解释这些位,您发送纯文本,JSON,XML甚至原始二进制数据都无关紧要。

It's worth noting that the payload has nothing to do with AJAX! Under the hood they are all using the XmlHttpRequest object to send an HTTP request asynchronously to the server. It doesn't matter if you send Plain Text, JSON, XML, or even raw binary data so long as you tell the server how to interpret those bits.

网址编码纯粹是< form> 元素在AJAX出现之前将数据发布到服务器的历史工件。

The url encoding is purely a historical artifact of how <form> elements posted their data to the server before AJAX was around.

这篇关于ajax发布数据是否需要进行URI编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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