locked
WinJS.xhr will not post, whatever I do.

    Question

  • Hi,

    So far, I've tried a bunch of examples found on this site, and in the WinJS reference.

            var url = "http://my-url.com/interface/mobile?api=login";
            var username = "test";
            var password = "test";
    
            var params = "username=" + username;
            params += "&password=" + password;
    
            WinJS.xhr({
                type: "POST",
                url: url,
                headers: { "Content-type": "application/x-www-form-urlencoded" },
                data: params
            }).then(function (response) {
                console.log(response.responseText);
    
            });

    This is the latest iteration of the code block for testing, but what ever I have tried so far, the method will only send a GET request, it will not send a POST request at all.

    What am I doing wrong?

    Many Thanks

    Stuart

    Sunday, November 04, 2012 11:58 AM

Answers


  • Hi Stuart,

    Here is a simple function which perform both HTTP get and post requests to a public url location. I've tested locally and it works. also, you can try using fiddler to inspect the HTTP traffic issued by the WinJS.xhr code to see what happend on wire

        function xhrTestHandler() {
    
            // HTTP GET
            var url = "http://sxp.microsoft.com/feeds/3.0/msdntn/VisualStudioNews";
    
            WinJS.xhr({
                url: url,
                type:"GET"
            }).then(
                function (resp) {
                    console.log(resp.statusText);
                },
                function (err) {
                    console.log(err.message);
                }
            );
    
    
            // HTTP POST
    
            WinJS.xhr({
                url: url,
                type: "POST",
                headers: {
                    "content-type": "xxx-form-urlencoded"
                },
                data:"x=1&y=2"
            }).then(
                        function (resp) {
                            console.log(resp.statusText);
                        },
                        function (err) {
                            console.log(err);
                        }
                    );
    
        }
    

    #fiddler web debugger
    http://www.fiddler2.com/fiddler2/


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. Putting communities in your palms. Launch the browser on your phone now, type aka.ms/msforums and get connected!

    • Marked as answer by Stoo2000 Monday, November 05, 2012 10:54 AM
    Monday, November 05, 2012 3:01 AM
    Moderator

All replies

  • So, I've tried a few more things.

    I've tried using XMLHttpRequest directly, and still, the server is receiving a GET request, not a POST.

    I've tried using one of the WinJS.xhr examples from the free eBook, just incase I've got a typo somewhere (although I've already tried many different samples). The result is the same, it's a GET not POST.

    Thanks again.
    Stuart

    Sunday, November 04, 2012 9:16 PM

  • Hi Stuart,

    Here is a simple function which perform both HTTP get and post requests to a public url location. I've tested locally and it works. also, you can try using fiddler to inspect the HTTP traffic issued by the WinJS.xhr code to see what happend on wire

        function xhrTestHandler() {
    
            // HTTP GET
            var url = "http://sxp.microsoft.com/feeds/3.0/msdntn/VisualStudioNews";
    
            WinJS.xhr({
                url: url,
                type:"GET"
            }).then(
                function (resp) {
                    console.log(resp.statusText);
                },
                function (err) {
                    console.log(err.message);
                }
            );
    
    
            // HTTP POST
    
            WinJS.xhr({
                url: url,
                type: "POST",
                headers: {
                    "content-type": "xxx-form-urlencoded"
                },
                data:"x=1&y=2"
            }).then(
                        function (resp) {
                            console.log(resp.statusText);
                        },
                        function (err) {
                            console.log(err);
                        }
                    );
    
        }
    

    #fiddler web debugger
    http://www.fiddler2.com/fiddler2/


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. Putting communities in your palms. Launch the browser on your phone now, type aka.ms/msforums and get connected!

    • Marked as answer by Stoo2000 Monday, November 05, 2012 10:54 AM
    Monday, November 05, 2012 3:01 AM
    Moderator
  • Doh!

    I didn't think to try fiddler, turns out that Apache was redirecting the request (adding a backslash), so the request became a GET.

    Thanks for pointing me in the right direction Steven.

    Stuart

    Monday, November 05, 2012 10:55 AM