我正在使用 webworks sdk 开发一个剧本应用程序。
我正在尝试通过发送和接收数据来发出 http 请求(方法:发布)。
我可以从服务器获取响应,但是服务器无法获取 $POST 数据,当我尝试显示 $_POST['apiKey'] 时,什么也没有,我检查了我的代码 100 次,检查了我的 config.xml 中的 uri ,找不到错误。
TL;DR:无法发送但可以接收数据。
我的 PHP 服务器代码:
echo "passed key is: ".$_POST["apiKey"]; // Nothing apears
echo "<br>";
if(md5($_SESSION['private_key'])===$_POST["apiKey"]){
}
else{
echo "Invalid API Key"; // Always getting this response on client app
exit();
}
?>
我的 JS 客户端代码:
function httpRequest(){
var key="a984a4474cff54d8468a296edf3af65b";
document.getElementById("status").innerHTML="Reaching server...";
//////////////////////////////////////
var xdr = getXDomainRequest();
xdr.onload = function() {
document.getElementById("status").innerHTML=xdr.responseText;
}
xdr.open("POST", "http://mydomain/index.php");
xdr.send("apiKey="+key);
}
解决了:
使用 POST 方法时,您应该定义请求 header :
xdr.open("POST", "http://mydomain.com/index.php");
xdr.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); // with this line
xdr.send("apiKey="+key);
最佳答案
已解决:使用 POST 方法时,您应该定义请求 header :
xdr.open("POST", "http://mydomain.com/index.php");
xdr.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); // with this line
xdr.send("apiKey="+key);
关于ajax - WebWorks AJAX 跨域请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11063321/