【问题标题】:How to send data to a web Service Using Winj.xhr如何使用 Winj.xhr 将数据发送到 Web 服务
【发布时间】:2013-03-23 22:08:01
【问题描述】:

我是Winjs 编码的新手,我在列表中有数据。 我想将该数据发送到我的 json Web 服务。

我成功调用了 Web 服务,并且我得到了响应,所以 Web 服务是 工作,但似乎没有发送数据。我不知道如何声明数据。

我有很多数据要发送(用户名、名字、姓氏、密码)到我的Register.json Register.json 执行后有这个响应:

{

"format":
"json",

"success":
false,

"errors":
["User name is empty"],

"result":
null

}

所以我确定没有发送数据

function Register() {
    var dataArray = [

   {
       username: "Marley",

       first_name: "dog",

       last_name: "ded",

       password: "pdre4252d"

   }];

    WinJS.xhr({

        url: "my_Base_URL/Register.json",

        type: "post",

        headers: { "Content-type": "application /x-www-form-urlencoded" },

        data: dataArray

      // data:JSON.stringify(dataArray)

    }).done(

          function complete(result) {
              if (result.status === 200) {
                  console.log("Success: Response Text: " + result.responseText);
              }
              else {
                  console.log("Fail:Response Text:  " + result.responseText);
              }
          },

          function error(error) {
              console.log("error");
             },
          function progress(result) {
            }

);
}

如果有人给我一些帮助,我会考虑周到的。

【问题讨论】:

  • Web 服务似乎需要用户名,但 WinJS.xhr 选项中没有提供用户名。该服务是否有任何身份验证要求?
  • 没有打开,现在用 XMLHttpReuest 没问题,我用 .send(data) 没问题,但我不知道它们之间的区别?

标签: javascript microsoft-metro windows-store-apps winjs


【解决方案1】:

WinJS.xhr() 是围绕XMLHttpRequest 的promise 包装器,这意味着它将您的参数委托给XMLHttpRequest 对象并返回WinJS.Promise 对象。参数可能无法正确委派,因此请尝试为用户名等添加和空字符串。否则,您可以通过创建自己的 WinJS.Promise 并在内部添加 XMLHttpRequest 来模仿相同的功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-29
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多