关于wex5 ajax跨域问题

             很多时候,我们都需要调用第三方的webservice接口来获取数据,这个时候,就存在跨域的问题,好在ajax提供了一种叫jsonp的数据格式,让跨域不在成为问题。直接上代码:

//天气查询
	Model.prototype.button1Click = function(event){
		var city = this.comp("input7").val();
		var data = this.comp("weatherMsg");
		data.clear();
		$.ajax({
			type:"get",
			dataType:"jsonp",
			async:"false",
			url:"http://api.map.baidu.com/telematics/v3/weather",
			data:{location:city,output:"json",ak:"49po7styrZWlXOkNGD1LUc1guxy7CwWx"},
			jsonp:"callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback) 
			jsonpCallback:"jsonpCallback", //自定义的jsonp回调函数名称"jsonpCallback",返回的json也必须有这个函数名称
			success:function(result){
				for(var i = 0;i<result.results[0].weather_data.length;i++){
					data.newData({				
						"defaultValues" : [ {
							"Id" : justep.UUID.createUUID(),
							"date" : result.results[0].weather_data[i].date,
							"dayPictureUrl":result.results[0].weather_data[i].dayPictureUrl,
							"nightPictureUrl":result.results[0].weather_data[i].nightPictureUrl,
							"weather":result.results[0].weather_data[i].weather,
							"wind":result.results[0].weather_data[i].wind,
							"temperature":result.results[0].weather_data[i].temperature,
							"currentCity":result.results[0].currentCity
						} ]
					});
			data.first();
				}
			}
		});
	};

以上是关于wex5开发的一个调用天气预报接口的跨域请求操作,其中dataType为jsonp格式,jsonp是json格式的扩展,如果指定了script或者jsonp类型,那么当从服务器收到数据室,实际上是用 了<script>标签而不是XMLHttpRequest对象,这种情况下,$.ajax()不在返回一个XMLHttpRequest对象,并且也不会传递事件处理函数,




  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值