locked
Sending input information to json Web Service with WinJs.xhr RRS feed

  • Question

  • Hi , i would like to know how to send information which are required for my web service.

    So i must send in the Winjs.xhr function all information from the <input> html interface.

    i declared an array

     var data=[];  

    et i push my data here:

    data.push(....);

    Then i added the function like that but i dont know where i put my data to be sent

    WinJS.xhr(
                {
                    url: baseUrl + "Register.json",
                    type: "POST",

                }
                ).done(
                    function completed(request) {
                        // handle completed download.
                    },
                    function error(request) {
                        // handle error conditions.
                    },
                    function progress(request) {
                        // report on progress of download.
                    });

     
    Tuesday, March 19, 2013 11:13 AM

Answers

  • Problem Resolved :

    WinJS.xhr({
                type: "post",
                url: "BaseURL/Register.json",
                headers: { "Content-type": "application/json" },
                data: JSON.stringify(p)
            }).done(

                  function succes(resultat) {
                      if (resultat.status === 200) {
                          console.log("success");
                          console.log(resultat.responseText);

    @jeff Sanders thx ;) 

    Tuesday, March 19, 2013 4:54 PM

All replies

  • Hi Brahim,

    If you search this forum for XHR and JSON you will find many solutions.  What you send depends on what the server expects.  Here is an example of sending an object:

    http://social.msdn.microsoft.com/Forums/en-US/winappswithhtml5/thread/fc970501-146c-48ac-91c7-18f8b24b3123\

    Search this forum to find what matches your situation closest.

    -Jeff


    Jeff Sanders (MSFT)

    Tuesday, March 19, 2013 12:05 PM
    Moderator
  • So i have  function that send request to web Service and this one should return a json format Response (a token):

           

    function Register()
        {
            var data = new FormData();

    //inputs of the Web Service

    var username = document.getElementById("username").value;
            var first_name = document.getElementById("first_name").value;
            var last_name = document.getElementById("last_name").value;
            var email = document.getElementById("email").value;
            var password = document.getElementById("password").value;
            var is_full_version = document.getElementById("is_full_version").value;

    //data that contains inputs informtion

            data.append("username", username);
            data.append("first_name", first_name);
            data.append("last_name", last_name);
            data.append("email", email);
            data.append("password",password);
            data.append("is_full_version", is_full_version);
            WinJS.xhr(
                {
                    url: "http://MyBaseURL/Register.json",
                    type: "POST",
                    //data: data,
                    data: JSON.stringify(data),

                }
                ).then(
    function (success) {
        document.getElementById("aff").textContent= "success "+JSON.parse(success.responseText);
    },
    function (error) {
        document.getElementById("aff").textContent ="error "+JSON.parse(error.responseText);
            }
            );}

     The Web Service return this ==>  Success [Object object]

    Any help will be appreciated

    Tuesday, March 19, 2013 1:59 PM
  • Hi Brahim,

    I am not sure what you need help with.

    Did you debug your application?  What do you see? What is the problem?

    -Jeff


    Jeff Sanders (MSFT)

    Tuesday, March 19, 2013 2:07 PM
    Moderator
  • so i want to see the  response to my request ; I think that the result is in this Object but i make a parse, it should be rendered .

    the response of my web service is like this:

    {"format":"json","success":false,"errors":["user name exisist"],"result":null}

    how can i access to those attributes?

     
    Tuesday, March 19, 2013 2:23 PM
  • Hi Brahim,

    Did you debug your application?  What did you see?  What is the value of success.responseText?

    -Jeff


    Jeff Sanders (MSFT)

    Tuesday, March 19, 2013 2:39 PM
    Moderator
  • Problem Resolved :

    WinJS.xhr({
                type: "post",
                url: "BaseURL/Register.json",
                headers: { "Content-type": "application/json" },
                data: JSON.stringify(p)
            }).done(

                  function succes(resultat) {
                      if (resultat.status === 200) {
                          console.log("success");
                          console.log(resultat.responseText);

    @jeff Sanders thx ;) 

    Tuesday, March 19, 2013 4:54 PM
  • Just so everyone is clear...

    The solution is to:

    1.  Debug your app and see what is being returned (see that it not returning the correct thing).

    2.  Use the headers: { "Content-type": "application/json" } to tell the service you are expecting JSON back

    The issue is that you have to tell the server what you want.  This problem really has to do with understanding what you have to do to get your data back from the server in JSON format and then using the correct headers to get that data.

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, March 20, 2013 11:01 AM
    Moderator