Ajax请求成功,但结果是空的
我建立一个使用 $。阿贾克斯
请求从网页数据发送到我的服务器,谷歌的Chrome浏览器扩展(目前使用托管本地主机)。正被在网页的上下文中执行的 content_script.js
文件(的详细内容脚本)的扩展访问运行该code:
I am building a Google Chrome browser extension that uses $.ajax
requests to send data from webpages to my server (currently hosted using localhost). The content_script.js
file that is being executed in the context of the webpages (more on content scripts) that the extension has access to runs this code:
//note: encode64String is assigned earlier in the script...
$.ajax({
type: "POST",
url: "http://localhost:8888/quartzsite/uploadendpoint.php",
type: "jsonp",
data: {img: encode64String},
contentType: "application/x-www-form-urlencoded;charset=UTF-8",
success: function(data){
console.log("The ajax request succeeded!");
console.log("The result is: ");
console.log(data);
},
error: function(){
console.log("The request failed");
}
});
的问题是,阿贾克斯
请求是成功的,但数据
参数,它返回空...
The problem is that the Ajax
request is succeeding but the data
argument that it returns is empty...
控制台看起来像这样运行后,code:
The console looks like this after the code is run:
目前在 uploadedendpoint.php
文件的内容是:
<?php
header("Access-Control-Allow-Origin: *");
echo 'This comes from php file'; die();
?>
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
The content of the document......
</body>
</html>
这意味着至少应该是什么东西在数据返回
变量。
This means there should be at least something being returned in the data
variable.
我进一步证实,请求成功,因为当我将请求发送到一个URL损坏(即uploaddddddendpoint.php)在 $。阿贾克斯
中的code >的错误
参数被执行。
I have further confirmed that the request is succeeding because when I send the request to a broken url (i.e. uploaddddddendpoint.php) the code inside the $.ajax
's error
parameter is executed.
我看过类似的问题,如jQuery $就响应空,但只有在Chrome浏览器但无济于事...
I have read similar questions like jQuery $.ajax response empty, but only in Chrome but to no avail...
更新
我已删除无效的第二个键入:JSONP
参数完全并增加数据类型:text / html的
。我现在得到一个失败 AJAX
要求每个code运行时。
I have removed the invalid second type: "jsonp"
parameter entirely and have added dataType: "text/html"
. I am now getting a failed ajax
request each time the code is run.
更新:奇怪的是改变数据类型:text / html的
到数据类型:HTML
引起 AJAX
请求成功,但再次空白数据
变量。
更新:当使用开发工具包来监视网络XHR这些都是发送/应答报文:
UPDATE: Strangely changing dataType : "text/html"
to dataType : "html"
causes the ajax
request to succeed but again with a blank data
variable.
UPDATE: When using the dev toolkit to monitor the Network XHR these are the sent/response messages:
至于可能出现重复的标志 Impossible在一个Chrome扩展跨站点AJAX API调用?我建议以其他方式!我已经调查这个问题,问题似乎并不相同。
With regards to the flag of possible duplication to Impossible to cross site ajax api calls in a chrome extension? I suggest otherwise! I have investigated that question and the problem does NOT seem to be the same.
为什么你在你的AJAX请求两个键入
字段? JSONP
和 POST
。
Why do you have two type
fields in your AJAX request? jsonp
and POST
.
$.ajax({
type: "POST", // OK
url: "http://localhost:8888/quartzsite/uploadendpoint.php",
type: "jsonp", // ???
// ...
});
更新:
我觉得你应该使用的URL的相对路径。试着改变你的要求如下:
I think you should be using the relative path for the URL. Try changing your request to the following:
$.ajax({
type: "POST",
url: "/quartzsite/uploadendpoint.php",
dataType: "text/html",
data: {img: encode64String},
success: function(data){
console.log("The ajax request succeeded!");
console.log("The result is: ");
console.dir(data);
},
error: function(){
console.log("The request failed");
}
});
您可以看到我换成 /quartzsite/uploadendpoint.php
的URL。这可能会解决这个问题......绝对URL可能预示着跨域请求,这是你没有什么后。
You can see I replaced the URL with /quartzsite/uploadendpoint.php
. This may solve the problem... the absolute URL might signal a cross-domain request which is not what you're after.
此外,作为一个侧面说明,这是没有必要设置的contentType,因为你将其设置到什么已经是默认值。如果你发送一个JSON或XML,那么你想要设置的contentType。
Also, as a side note, it's unnecessary to set the contentType, since what you're setting it to is already the default value. If you were sending a JSON or XML, then you'd want to set the contentType.