gpt4 book ai didi

ajax - 使用 jquery 将 & 符号传递给 ajax?

转载 作者:行者123 更新时间:2023-12-01 00:45:09 25 4
gpt4 key购买 nike

我有这个ajax调用

 function addNewRemarksToDataBase(argRemark) {
if (argRemark != '') {
// if not blank
$.ajax({
url: '../AutoComplete.asmx/AddNewRemarks',
type: 'POST',
timeout: 2000,
datatype: 'xml',
cache: false,
data: 'argRemarks=' + argRemark,
success: function (response) {
// update the field that is source of remarks
updateRemarksSource();
},
error: function (response) {
}
});
}
};

该方法定义为

[WebMethod]
public void AddNewRemarks(string argRemarks)
{
BAL.BalFactory.Instance.BAL_Comments.SaveRemarks(argRemarks, Globals.BranchID);
}

问题是,如果用户输入类似 long & beautiful 或类似 smart & beautiful 的内容,包含 & 的内容,我只会获取 & 之前的第一部分,long (在第一种情况下),smart (在第二个情况下)(还要注意空格!)

我读到jquery ajax documentation应该将 processData 设置为 false,因为它用于查询字符串或其他内容。我添加了

processData: false

但我仍然得到 & 之前的术语。我不想使用 encodeURIComponent 因为它会将 & 转换为 amp; (或类似的东西)。我需要的是完整的值long &优雅smart & beautiful将保存到数据库中。我怎样才能做到这一点?

编辑 执行 { argRemarks: argRemark } 没有帮助!该函数不会被调用。使用 firebug 运行它,并在错误函数中设置断点,我得到了这个

[Exception... "Component does not have requested interface"  nsresult: "0x80004002 (NS_NOINTERFACE)"  location: "JS frame :: http://localhost:49903/js/jquery-1.8.1.min.js :: .send :: line 2"  data: no]"

更新 2:正在做

data: 'argRemarks=' + encodeURIComponent(argRemark)

成功了。但任何人都可以帮助我理解这是如何工作的吗?我以为它会将 & 转换为 & 但事实并非如此?我现在收到的方法参数正是我想要的,长而优雅智能而美丽,不 encodeURIComponent() 转换特殊字符?

最佳答案

确实需要对argRemark进行编码。最简单的方法是让 jQuery 为您完成这项工作:

data: { argRemarks: argRemark }

这与 data: 'argRemarks=' + argRemark 不同,通过传入一个对象,jQuery 假定它需要对该对象的属性值进行 URL 编码 -- 而如果传入一个字符串,您应该事先对其进行正确编码。

关于ajax - 使用 jquery 将 & 符号传递给 ajax?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15086354/

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