我正在创建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>

最佳答案

原因:

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";

关于javascript - Windows 8 HTML 5 Web APP AJAX发布不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16495335/

10-13 07:47
查看更多