【问题标题】:Winjs get request failing to return dataWinjs获取请求无法返回数据
【发布时间】:2013-09-01 17:27:07
【问题描述】:

我遇到了一个奇怪的问题。在我的应用程序中,我有以下代码

WinJS.xhr({
                url: 'http://bdzservice.apphb.com/api/Route?fromStation=София&toStation=Варна&date=30/08/2013&startTime=00:00&endTime=24:00'

            }).then(function (success)
            {
                console.log(success);
            },
            function (error)
            {
                console.log(error);
            }
            );

问题是我得到一个空的响应文本(状态为 200)。我提供的网址通过浏览器和其他休息客户端返回数据,但在应用程序中我没有得到任何数据。问题可能出在哪里?

【问题讨论】:

    标签: ajax windows-8 winjs windows-store


    【解决方案1】:

    您需要通过encodeURIComponent 对查询字符串参数进行编码(浏览器在粘贴网址时会自动为您执行此操作)。

    以下代码可以解决问题:

    function serialize (obj) {
    var str = [];
    for (var p in obj) {
        if (obj.hasOwnProperty(p)) {
             str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
         }
     }
    
     return str.join("&");
    };
    
    var request = {
        fromStation: 'София',
        toStation: 'Варна',
        date: '30/08/2013',
        startTime: '00:00',
        endTime: '24:00'
    };
    WinJS.xhr({
        url: 'http://bdzservice.apphb.com/api/Route?' + serialize(request)
    }).then(function(success) {
        console.log(success);
    },
        function(error) {
            console.log(error);
        }
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-29
      • 2020-03-25
      • 1970-01-01
      • 2019-11-24
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 2015-06-28
      相关资源
      最近更新 更多