我正在开发一个移动应用程序,该应用程序将连接到网站API。我通过jQueryAjax发送数据,但在将数据转义为URL友好时,我的GET参数在到达API时变为空。
我有以下jQuery/javascript代码
var id = 012345;
var token = 55fggt98ujhsbnh;
var email = "sample@example.com";
var json = { // Create object
"task" : "request",
"id" : id,
"token" : token
};
var jsonfull = []; // Push to jsonfull.
jsonfull.push(json);
var url = JSON.stringify(json);
var crypt = '&hash='+CryptoJS.SHA512(token+task+email+url).toString(CryptoJS.enc.Hex)+'&callback=?';
var x = 'json='+escape(url)+escape(crypt);
$.ajax({
url: "mydemosite.com/?p=app&",
crossDomain: true,
data: x,
dataType: "jsonp"
})
.done(function (json){
alert(json.response);
});
在发送数据的php代码上,我有以下代码来接收ajax数据
$rest = $_GET['json'];
$api = json_decode($rest,true);
$type = 'json';
file_put_contents('logs/api_task.txt', print_r($_GET,true));
检查保存内容的文件我得到了这个
Array
(
[p] => app
[callback] => jQuery1111006607784540392458_1446159115404
[json] => {"task":"balance","merchant":"hifee","token":"1446159118-5632a30e2ed0f5632a30e2ed465614257"}&hash=0b645fac1f3ef3b5911435e58fbc1f157bd0273a01a664427b014fb517d9c46ae60e1d2fe3128d76d610706f9b8a9ac660517b77cbf4c3c6e02e597b065d1038&callback=?
[_] => 1446159115406
)
我希望得到的数据是
Array
(
[p] => app
[callback] => jQuery1111006607784540392458_1446159115404
[json] => {"task":"balance","merchant":"hifee","token":"1446159118-5632a30e2ed0f5632a30e2ed465614257"}
[hash] => 0b645fac1f3ef3b5911435e58fbc1f157bd0273a01a664427b014fb517d9c46ae60e1d2fe3128d76d610706f9b8a9ac660517b77cbf4c3c6e02e597b065d1038
)
如果能得到任何帮助,我将不胜感激。
因为您正在转义crypt
变量并将其连接到JSON参数值。使用escape
,您还转义了&
,所有内容都成为json
参数的一部分。
如果您只是将参数作为对象传递,并让jQuery转义这些值,则会更简单。
$.ajax({
url: "mydemosite.com",
crossDomain: true,
data: {
p: "app",
json: url,
hash: CryptoJS.SHA512(token+task+email+url).toString(CryptoJS.enc.Hex)
},
dataType: "jsonp"
})
.done(function (json){
alert(json.response);
});