简体   繁体   English

Windows 8 HTML 5 Web APP AJAX发布不起作用

[英]Windows 8 HTML 5 Web APP AJAX post not working

I am creating Windows Phone 8 HTML 5 App. 我正在创建Windows Phone 8 HTML 5应用程序。 I trying to ajax post for getting weather information. 我试图通过ajax发布获取天气信息。 But i am not getting any response. 但是我没有得到任何回应。 I am unable to trace the problem behind it. 我无法找到背后的问题。

$(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 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>

Reason : 原因:

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

You are trying to do AJAX cross-domain. 您正在尝试做AJAX跨域。

EDIT 编辑

Example of a, here in php, proxy.php : 一个示例,在php中,proxy.php:

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

Then you call the ajax as 然后您将ajax称为

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

The reason I mentioned callback=? 我提到callback=?的原因callback=? in my comment above is because weatheronline.com supports JSONP which does support cross-domain requests. 在我的评论以上是因为weatheronline.com支持JSONP它支持跨域请求。

This works for me from PC browser. 这从PC浏览器为我工作。 I don't have a Windows 8 phone, so I can't test: 我没有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));
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM