我正在尝试从 Chrome 扩展程序向使用 Google App Engine 托管的服务器发送最小的 POST 请求。这是我的服务器代码:
class Receive(webapp2.RequestHandler):
def get(self):
logging.info('received a get')
def post(self):
logging.info('received a post')
这是我的 Chrome 扩展程序中的 Javascript 代码:
$.post("http://perativ.com/receive");
$.ajax({
type: "POST",
dataType: "JSON",
url: "http://perativ.com/receive"
});
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://perativ.com/receive");
xhr.send();
我有<all_urls>
我的许可manifest.json
文件。
我添加了三个相同的请求以表明没有任何效果。当我运行此 Chrome 扩展程序,然后检查 perativ.com 上的服务器日志时,我收到三行内容:“已收到 get”。
感谢所有帮助,谢谢!
最佳答案
通过以下命令可以看到,对您网站的请求正在使用 HTTP 状态代码 301 从 perativ.com 重定向到 www.perativ.com。发生此重定向时,POST 将转换为 GET。
要解决此问题,请将请求发送到 www.perativ.com 而不是 perativ.com,或者禁用重定向(如果在您的范围内)。
$ curl -X POST http://perativ.com/receive -H 'Content-Length: 0'
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.perativ.com/receive">here</A>.
</BODY></HTML>
关于javascript - 从 Chrome 扩展程序到 App Engine 的 POST 请求作为 GET 请求收到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25417915/