我使用pmxdr庫進行跨域調用(jQuery到php並在json中響應)。問題是我不能正確處理響應,但如果我只是將它打印在HTML的當屬 -無法處理從php
{"title":"Mr","first_name":"Shak","last_name":"Mana"}
下面是它的代碼我得到上面提到的控制檯上使用
pmxdr.request({
uri : "http://xxxx/pmxdr/respons1.php",
callback: handleResponse
});
function handleResponse(response) {
if (!response.error) { // request successful
console.log(response.headers["content-type"]) //works
console.log(response.data) //works
for (var key in response.data) {
alert(response.data[key]); // gives each character :(
}
} else print("Error: " + response.error);
}
JSON,但在警報中,我將每個字符分開彈出。如果我使用console.log(response.data["title"])
它說未定義。請告訴我我做錯了什麼。
您的'response.data'是一個字符串。 [解析你的'response.data'到json](http://stackoverflow.com/q/4935632/155813)。 – mg007
document.write(JSON.stringify(response.data)); –