我真的谷歌了很多,但没有找到涵盖我的问题的答案。
我正在开发一个chrome扩展,我想通过jquery ajax调用。
我是这样写的:
popup.js
$(function()
{
$('#btn').click(function()
{
$('#wait').html('loading...');
jQuery.getJSON("http://domain.com/extension_php_files/generate.php?callback=?",
{id:25},
function(data)
{
$('#wait').html('');
console.log( JSON.stringify(data) )
$.each(data, function(key, val)
{
alert(key + ' ' + val);
});
});
});
});
这里是我的popup。html
<!doctype html>
<html>
<head>
<title></title>
<script src="jquery.js"></script>
<script src="popup.js"></script>
</head>
<body>
<a href="#" id="btn">SEND</a><br /><br /><br /><br />
<span id="wait"></span>
</body>
</html>
和 manifest.json
{
"manifest_version": 2,
"name": "black",
"description": "black here",
"version": "1.0",
"permissions": [
"http://domain.com/*"
],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
}
}
当我点击SEND
按钮时,我看到Loading...
,但它从未进入成功功能。我用谷歌搜索了一下,但没有找到问题所在。
我做错了什么?
PS:我的服务器端代码工作得很好
我有时也很难使用jquery ajax请求,我最好的解决方案就是用原生javascript ajax代码替换该代码。尝试使用XMLHttpRequest发出请求,如下所示:
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://domain.com/", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
// paste your code here
}
}
xhr.send();
将http://domain.com/替换为完整的url
感谢Rob W的帮助。
我在url的末尾删除了?callback=?
。在服务器端,我做了这样的操作:
$id = $_GET['$id'];
$arr = array ("id"=>$id,"name"=>"Siamak","lname"=>"Aghaeipour","age"=>"24");
$jsonData = json_encode($arr);
echo $jsonData;