问题
I'm having abit of problems posting data from WinJS.xhr to a PHP script. "obj" is a stringified JSON object
WinJS.xhr({
type: "POST",
url: dataUrl,
headers: { "Content-type": "application/x-www-form-urlencoded" },
data: obj,
})
However the $_POST variable is always empty.
I've tried changing content-types, and escaping the object but no luck :(
回答1:
Your content-type when posting json should typically be application/json
Secondly make sure you 'stringify' your json object.
Based on: Post JSON data to web services in Windows 8
WinJS.xhr({
type: "post",
url: dataUrl,
headers: { "Content-type": "application/json" },
data: JSON.stringify(obj)
})
回答2:
Figured out a soloution.
Incase anyone has the same problem i got it working by removing headers from the xhr, and getting the post data @ server side with this code:
$data = file_get_contents('php://input');
$data = (array) json_decode($data);
来源:https://stackoverflow.com/questions/19007540/posting-json-object-through-winjs-xhr