ajax - WebWorks AJAX 跨域请求

标签 ajax blackberry xmlhttprequest blackberry-playbook blackberry-webworks

我正在使用 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/

相关文章:

javascript - 在javascript中从服务器读取.svg文件

jquery - Rails 新操作在 AJAX GET 上不返回 HTML

java - 黑莓 java ListField

javascript - 键盘在 Android 和 BlackBerry 中未显示在元素焦点上

AngularJS 在 POST 上从 https 请求 http

javascript - XMLHttpRequest 在 WordPress 中不起作用

javascript - 从 View 中的脚本调用 Controller 操作

jquery - ASP.NET MVC 5 中的 Ajax(部分 View )不起作用 - 始终仅单独打开部分 View

javascript - 使用ajax加载onsen-ui内容

eclipse - 为 BlackBerry 开发设置 Eclipse