javasciprt下jquery函数$.post执行无响应的解决方法


在编写javascirpt程序过程中,用$.post方法发送数据,若数据中字符含有'<‘,将导致$.post无法成功执行。
复制代码 代码如下:

var jsonstr='{"value":"abcd<efg"}';
$.post(
url,
{ "jsonstr": jsonstr },
function (data, status) {
});

需要将其转义后再使用,使用下面的transferredChars函数转义后,再传递数据$.post即能执行。

此函数使用将'<'和‘>'分别替换为'<'和‘>'。
复制代码 代码如下:

transferredChars=function (htmlChars) {
var tcs = htmlChars.replace(/</g, "<");
tcs = tcs.replace(/>/g, ">");
return tcs;
}

复制代码 代码如下:

var jsonstr='{"value":"abcd<efg"}';
jsonstr=transferredChars(jsonstr);
$.post(
url,
{ "jsonstr": jsonstr },
function (data, status) {
});

使用的jquery版本为1.7.1.min
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3