简体   繁体   中英

Windows 8 HTML 5 Web APP AJAX post not working

I am creating Windows Phone 8 HTML 5 App. I trying to ajax post for getting weather information. 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

    <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.

EDIT

Example of a, here in 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

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=? in my comment above is because weatheronline.com supports JSONP which does support cross-domain requests.

This works for me from PC browser. I don't have a Windows 8 phone, so I can't test:

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));
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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