在使用ajax的过程中,应用POST请求发送一个JSON的String给后台的时候,发现出现了一个发送出的字符串中的标点符号都转换成URL编码表的状态。
以下是我当时写的测试代码
function Submit() {
var account = document.getElementById("login_account").value;
var password = document.getElementById("login_password").value;
var item={
"account":account,
"password":password
};
var jsonString = JSON.stringify(item);
alert("json_str = " + jsonString);
$.ajax({
type:"POST",
url:"http://127.0.0.1:8080/zhanghu/login/post",
data:jsonString,
dataType:"json",
success: function (result) {
console.log(result);
alert('成功了');
}
});
其中第一个alert显示如下:
而后台接受到的却变成了
经过比对发现是{“”}之类的字符在传输过程中变成了URL编码表之后的模样,
如{变成了%7B
解决方法是在$ajax中加入
contentType: "application/json",