我正在尝试将带有主题标签的网址发送到服务器,例如
/example.php?var1=tes#t&var2=value2
如果我在beforeSend函数中跟踪该URL,我会看到该URL仅
/example.php?var1=tes
主题标签和包含主题标签之后的所有内容均被删除。如果我在使用$ .get()函数之前将主题标签编码为%23,则一切正常。
这是示例代码。
$.ajaxSetup(
{
scriptCharset: "iso-8859-1",
contentType: 'Content-type: text/plain; charset=iso-8859-1',
cache: true,
//zuerst alles encodieren, damit server keine fehler bekommt
beforeSend: function(xhr, data)
{
//wrong url
console.log(data.url);
}
});
//right url
var link = "/example.php?var1=tes#t&var2=value2";
$.get(link).done(function()
{
console.log("done");
})
编辑:
服务器使用iso-8859-1解码网址,因此我需要将数据编码为iso。我在beforeSend函数中编写了一个脚本,该脚本自动将url转换为iso,但不能转换井号,因为data.url变量不包含井号。因此,我需要在$ .ajaxSetup函数中访问完整的URL(包括井号)。
#后面的字符被视为哈希,而不是查询字符串的一部分。您需要对特殊字符进行编码。您应该使用encodeURIComponent来做到这一点。
但可喜的是jQuery在get方法中内置了$ .param()方法,它将为您完成转换。
var link = "/example.php";
$.get(link, { "var1" : "tes#t", "var2" : "value2" }). done(...);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句