Posting JSON object through WinJS.XHR

亡梦爱人 提交于 2019-12-14 02:43:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!