【问题标题】:Windows 8 HTML 5 Web APP AJAX post not workingWindows 8 HTML 5 Web APP AJAX 帖子无法正常工作
【发布时间】:2013-05-05 21:50:16
【问题描述】:

我正在创建 Windows Phone 8 HTML 5 应用程序。我试图通过 ajax 发布获取天气信息。但我没有得到任何回应。我无法追踪其背后的问题。

$(document).ready(function () {

        var apiUrl = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=London&format=json&num_of_days=5&key=KEY GOES HERE";

        //CALL BACK FUNCTION

        function mapWeather() {
            $("#contentPanel").text("111");
            $.ajax({
                url: apiUrl,
                type: 'GET',
                success: function (data) {
                    $("#contentPanel").text("adfdf");
                }
            });
        }
});

HTML

    <div id="row-fluid">
        <div class="input-append">
            <input type="text" id="searchCity" />
            <button type="button" id="addCity" unselectable="on" class="btn btn-primary" onclick="mapWeather()">+</button>
        </div>
<div id="contentPanel">
        testing
    </div>
    </div>

【问题讨论】:

    标签: javascript jquery html ajax windows-phone-8


    【解决方案1】:

    我在上面的评论中提到 callback=? 的原因是因为 weatheronline.com 支持 JSONP,而 确实 支持跨域请求。

    这适用于我的 PC 浏览器。我没有 Windows 8 手机,所以无法测试:

    var apiUrl = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=London&format=json&num_of_days=5&key=KEY HERE&callback=?";
    
    $("#addCity").click(function () {
       mapWeather(); 
    });
    
    //CALL BACK FUNCTION
    function mapWeather() {
        $("#contentPanel").text("111");
        $.ajax({
            url: apiUrl,
            type: 'GET',
            dataType: 'jsonp',
            success: function (data) {
                $("#contentPanel").text(JSON.stringify(data));
            }
        });
    }
    

    【讨论】:

    • 您使用的是 JQuery 2.x 吗?这支持 Windows 8 应用程序。见这里:net.tutsplus.com/tutorials/javascript-ajax/…
    • @是的,我正在使用jquery-2.0.0.min.js
    • 它在链接中以No More Same-Origin Issues 给出,但我遇到了这个问题
    • 也许可以尝试与 JQuery 人员联系以提供帮助 - 让他们知道它在 Chrome 中有效,但在 Windows 8 应用程序中无效:bugs.jquery.com/newticket
    【解决方案2】:

    原因:

    is not allowed by Access-Control-Allow-Origin.
    

    你正在尝试做 AJAX 跨域。

    编辑

    一个例子,这里是php,proxy.php:

    <?
    $url=$_SERVER['QUERY_STRING'];
    $from=strpos($url, 'url=')+4;
    $url=substr($url, $from);
    echo utf8_decode(file_get_contents($url));
    ?>
    

    然后你调用ajax为

    var apiUrl = "proxy.php?url=http://api.worldweatheronline.com/free/v1/weather.ashx?q=London&format=json&num_of_days=YOURKEY";
    

    【讨论】:

    • 你需要做一个代理,服务器端
    • 你能给我举个例子吗?
    • +1 用于跟踪错误,哦,我需要添加服务器端编码。我只是想在手机本身部署应用程序。我只是不喜欢 2 去服务器端。有没有其他办法?
    • 如果你可以控制服务器,你可以试试这个stackoverflow.com/a/6871067/1407478,或者看看这个,jquerymobile.com/demos/1.0.1/docs/pages/phonegap.html,allowCrossDomainPages,虽然我从来没有试过。
    • 嗨,谢谢,我已经听说过 phonegap。让我试试看。所以无论如何,对于我的问题,这个问题与跨域有关,所以我接受你的回答。谢谢...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多