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

 更新时间:2014年03月13日 17:44:48   作者:  
这篇文章主要介绍了javasciprt下jquery函数$.post执行无响应的解决方法,需要的朋友可以参考下
在编写javascirpt程序过程中,用$.post方法发送数据,若数据中字符含有'<‘,将导致$.post无法成功执行。
复制代码 代码如下:

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

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

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

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

复制代码 代码如下:

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

使用的jquery版本为1.7.1.min

相关文章

最新评论