locked
Windows store app xhr post does not work

    Question

  • I have this simple piece of code bellow in my app. I am trying to communicate with php file on localhost and simple return the same value as I send from app, but php keeps returning me nothing, but if I try to echo fixed string, then php return it correcty. I don't know, what have I done wrong.

    Javascript in windows store app:

    var login = {}, options = {};
    login.username = "username";
    login.password = "password";
    options.url = "http://localhost/api/";
    options.type = 'POST';
    options.data = JSON.stringify(login);
    options.headers = { "Content-Type": "application/json;charset=utf-8" };
    
    WinJS.xhr(options).done(loginSuccess, onError);
    function loginSuccess(result) {
        console.log(JSON.stringify(result));
    }
    function onError(er) {
        console.log(JSON.stringify(er))
    }

    Really simple php:

    echo json_encode($_POST['username']); //does not work
    echo "username"; //fixed value works

    Tuesday, November 19, 2013 9:42 AM

Answers

  • I think this could help you out as it works for me:

    WinJS.xhr({
                type: "POST", url: "http://localhost/api/",
                headers: { "Content-Type": "application/x-www-form-urlencoded" },
                data: "username=" + username
                 + "&password=" + password
            })

    and php

    $username = isset($_POST['username']) ? mysqli_real_escape_string($link, $_POST['username']) : '';
    $password = isset($_POST['password']) ? mysqli_real_escape_string($link, $_POST['password']) : '';

    //then you can complete your query and return your username

    good luck

    Sunday, November 24, 2013 3:59 PM

All replies

  • The best approach to troubleshoot this issue is to use a tool such as Fiddler, create a HTTP request manually and send the request to the server. Based on how the server requires the specific format of the HTTP request such as HTTP headers etc, you will need to create the XHR appropriately. You can use this approach to create the HTTP request using the "Composer" option of Fiddler: http://www.debugtheweb.com/Fiddler/help/composer.asp


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Tuesday, November 19, 2013 7:02 PM
    Moderator
  • Thanks for reply. I will try to do this, thanks, but anyway, it's strange, that it works with jQuery just fine. When I loose every hope, I just add jQuery in my app, just because of this issue, but it work's now at least.
    Tuesday, November 19, 2013 9:14 PM
  • I think this could help you out as it works for me:

    WinJS.xhr({
                type: "POST", url: "http://localhost/api/",
                headers: { "Content-Type": "application/x-www-form-urlencoded" },
                data: "username=" + username
                 + "&password=" + password
            })

    and php

    $username = isset($_POST['username']) ? mysqli_real_escape_string($link, $_POST['username']) : '';
    $password = isset($_POST['password']) ? mysqli_real_escape_string($link, $_POST['password']) : '';

    //then you can complete your query and return your username

    good luck

    Sunday, November 24, 2013 3:59 PM