asp.net – 如何使用ajax调用跨域web api?
发布时间:2020-09-25 16:53:03 所属栏目:asp.Net 来源:互联网
导读:jQuery.ajax({ type: GET, url: http://example.com/restaurant/VeryLogin(username,password), dataType: json, success: function (data) { a
jQuery.ajax({
type: "GET",url: 'http://example.com/restaurant/VeryLogin(username,password)',dataType: "json",success: function (data) {
alert(data);
},error: function (XMLHttpRequest,textStatus,errorThrown) {
alert("error");
}
});
它提醒成功,但数据为空. url返回xml数据,如果我们指定dataType,我们可以获取json数据,但是这里没有获取任何数据. 任何帮助赞赏. 解决方法Javascript受相同域策略的约束.这意味着为了安全起见,客户端浏览器中的JS脚本只能访问它来自的相同域.JSONP不受相同的限制. 在这里查看JSONP上的jQuery文档: http://api.jquery.com/jQuery.getJSON/ 以下是使用JSONP通过JQuery AJAX访问跨域服务的工作示例: http://jsbin.com/idasay/4 以防JSBIN将来删除此粘贴: jQuery.ajax({
type: "GET",url: 'http://api.geonames.org/postalCodeLookupJSON?postalcode=6600&country=AT&username=demo',dataType: "jsonp",cache: false,crossDomain: true,processData: true,success: function (data) {
alert(JSON.stringify(data));
},errorThrown) {
alert("error");
}
}); (编辑:长春站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-web-api – WebApi2属性路由404
- asp.net-mvc-3 – 如何在页面提交ASP.Net MVC时捕获哪个单选
- 如何将数组从Asp.net服务器端传递到客户端的Javascript函数
- 在asp.net中使用eval(“”)
- 如何在不使用MembershipProvider的情况下使用ASP.NET登录控
- asp.net-mvc – MVC DB首先修复显示名称
- 从ASP.NET C#启动一个程序
- asp.net – IE8 Win7 Facebook Connect问题
- asp.net 根据汉字的拼音首字母搜索数据库(附 LINQ 调用方法
- asp.net-mvc – 无法在Web服务器上启动调试.调试器无法连接
