gpt4 book ai didi

jquery - 对 jquery ajax 发送的字符串中的所有特殊字符进行转义

转载 作者:行者123 更新时间:2023-12-03 21:49:31 25 4
gpt4 key购买 nike

我尝试在执行 contentType: "application/json; charset=utf-8", ajax post 到 Web 服务时以键值对形式发送文本。我面临的问题是,如果其中一个参数(接受来自用户的文本)带有引号(“),它会破坏代码 [错误消息:传入的对象无效]。到目前为止,我已经尝试过这些但没有成功

var text = $("#txtBody").val(); 
var output1 = JSON.stringify(text);
var output2 = text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");

关于如何转义 jquery ajax 帖子的特殊字符有什么想法吗?

最佳答案

为什么不使用转义

escape(text);

https://developer.mozilla.org/en/DOM/window.escape

编辑!!!

正如评论中提到的,这已被弃用。

The deprecated escape() method computes a new string in which certain characters have been replaced by a hexadecimal escape sequence. Use encodeURI or encodeURIComponent instead.

请改用以下其中一项:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

关于jquery - 对 jquery ajax 发送的字符串中的所有特殊字符进行转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10133082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com