我正在对php进行跨域调用,返回此内容:
response=2&count=15&id=84546379&firstname=adsa&emailstatus=
我想挑选出响应和id的值,但不确定如何,我的代码如下:
**
xhr.request({
url: "../promo_getstate2.php",
method: "POST",
data: {
email: emailaddress,
country: country,
lang: lang,
source: '1312_XMAS_dly'
}
}, function(response){
getstate = response['response'];
regID = response['id'];
console.log(getstate)
console.log(regID)
})
但它并没有考虑这些价值观。我该怎么做?
回复是:
" response=2&count=15&id=84546379&firstname=adsa&emailstatus="
**
答案 0 :(得分:2)
您可以做的是在响应中创建所有参数的params
对象,如下所示:
function parseResponse(str) {
var arr = str.split("&");
var temp, params = [];
for(var i = 0; i < arr.length; ++i) {
temp = arr[i].split("=");
params[temp[0]] = temp[1];
}
return params;
}
var values = parseResponse("response=2&count=15&id=84546379&firstname=adsa&emailstatus=")
然后您可以访问值:
values['response']; // 2
values['id']; // 84546379
答案 1 :(得分:0)
代码:
<Script language="javascript">
var vars=GetRequest("response=2&count=15&id=84546379&firstname=adsa&emailstatus=");
document.write(JSON.stringify(vars));
function GetRequest(v) {
var url = "?"+v;//location.search;
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for(var i = 0; i < strs.length; i ++) {
theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}
</script>
然后你就可以获得价值。例如,结果的json是
{"response":"2","count":"15","id":"84546379","firstname":"adsa","emailstatus":""}