我正在构建一个 Google Chrome 浏览器扩展,它使用$.ajax
请求将数据从网页发送到我的服务器(当前使用本地主机托管)。这content_script.js
在网页上下文中执行的文件(有关内容脚本的更多信息 http://developer.chrome.com/extensions/content_scripts.html)扩展程序有权访问运行此代码:
//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");
}
});
问题是Ajax
请求成功,但是data
它返回的参数为空...
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>
这意味着至少应该有一些东西被返回data
多变的。
我进一步确认请求成功,因为当我将请求发送到损坏的网址(即 uploaddddddendpoint.php)时,$.ajax
's error
参数被执行。
我读过类似的问题,例如jQuery $.ajax 响应为空,但仅在 Chrome 中 https://stackoverflow.com/questions/3068808/jquery-ajax-response-empty-but-only-in-chrome但无济于事...
UPDATE:
我已经删除了无效的第二个type: "jsonp"
参数完全并添加了dataType: "text/html"
。我现在失败了ajax
每次运行代码时请求。
UPDATE: 奇怪的变化dataType : "text/html"
to dataType : "html"
导致ajax
请求成功但再次出现空白data
多变的。UPDATE:使用开发工具包监控网络 XHR 时,这些是发送/响应消息:
关于可能重复的标志无法在 Chrome 扩展中跨站点 ajax api 调用? https://stackoverflow.com/questions/11515031/impossible-to-cross-site-ajax-api-calls-in-a-chrome-extension我建议不然!我已经调查了这个问题,问题似乎并不相同。