1、利用后台文件去请求跨域连接,来达到实现跨域的目的
$().ready(function() {
createCity();
$("#city").bind('change', function() {
//城市代码
var code = this.value;
var data = {
code : code
};
$.post('create.php', data, function(msg) {
$('#result').append('城市:'+msg.weatherinfo.city+'<br>');
$('#result').append('日期:'+msg.weatherinfo.date_y+'<br>');
$('#result').append('温度:'+msg.weatherinfo.temp1+'<br>');
}, 'json');
});
});
function createCity() {
$.post('createCity.php', function(msg) {
$(msg).each(function(i, item) {
var op = new Option(item.name, item.code);
$("#city")[0].options.add(op);
});
}, 'json');
}
<?php
$code = $_POST['code'];
$file = "http://m.weather.com.cn/atad/" . $code . "html";
$content = file_get_contents( $file );
echo $content;