jquery - 数据中带有 location.href 的 jquery ajax 帖子不起作用
问题描述
如果使用(或)作为数据,是什么阻止类型POST
获得预期结果?location.href
window.location.href
此代码不起作用(它不返回任何内容):
$.ajax({
type: "POST",
url: 'page.asp',
data: {'q': location.href},
success: function(short_link,status){
console.log('short_link = '+short_link);
console.log('status = '+status);
}
});
我确实看到q值是在浏览器网络信息中发送的,但不知何故,由于302 对象移动状态代码,POST 似乎被重定向到 GET 。就像那里所说的那样:返回重定向作为对 XHR 请求的响应
但是,如果我将其更改为data: {'q': 'to some string'}
它可以工作。
此外,如果我更改要使用的代码GET
(以及相应的代码'page.asp'
),它也可以工作:
$.ajax({
type: "GET",
url: 'page.asp?q='+location.href,
success: function(short_link,status){
console.log('short_link = '+short_link);
console.log('status = '+status);
}
});
解决方案
我认为您必须对 url 进行编码,以替换特殊字符。
推荐阅读
- react-native - 我正在尝试从子组件打开模式
- flutter - 使用flutter ListView builder抛出错误
- office-ui-fabric - 在 Office UI Fabric 详细信息列表中选择新添加的行
- django - 注册 django 应用程序时出现 ModuleNotFoundError
- java - 请解释一下这个输出是怎么来的?
- python - 为什么 PyCharm 会自动超链接我的 URL
- reactjs - React Native 应用程序中的未知 HTTP GET 请求
- cocoa - 如何在没有动画的情况下滚动RowToVisible
- android - 如何使用 volley 在 android 中解析以“/”开头的 Json?
- java - 在 Java 8 中将 POJO 转换为 Tree 结构,以便在层次结构中表示数据