jquery ajax跨域访问webservice配置
2016-04-08 18:34
148 查看
1.webservice方法
[System.Web.Script.Services.ScriptService] public class TestService : System.Web.Services.WebService { [WebMethod] public string Test(string inputStr) { ////HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); return "Hello :"+inputStr; } }
2.webservice网站web.config配置
<?xml version="1.0" encoding="utf-8"?><!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<customErrors mode="Off"/>
<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpPost"/>
<add name="HttpGet"/>
<add name="Documentation"/>
</protocols>
</webServices>
</system.web>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/>
<add name="Access-Control-Allow-Headers" value="x-requested-with,content-type"/>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration> 图中框出的2块经测试 缺一不可
3.jquery ajax调用
function doClick1() {jQuery.support.cors = true; //IE10以下
$.ajax({ type: "post", url: "http://xx.com/TestService.asmx/Test", dataType: 'xml', data: { inputStr: 'everyone' }, success: function (data) { alert(data); //xml对象 }, error: function (XMLHttpRequest, textStatus, errorThrown) { console.log(XMLHttpRequest); alert('error:' + errorThrown); } }); }
From: http://www.cnblogs.com/xuejianxiyang/p/5369346.html
相关文章推荐
- 【JQuery】图片放大镜插件——jqzoom
- 【JQuery】图片灯箱插件——lightBox
- 关于JQuery 中表单提交中使用post方法,return false 不起作用解决办法
- jQuery的观察者模式详解 转载
- 【JQuery】表单插件——form
- HTML —— jquery如何判断checkbox(复选框)是否被选中
- 【JQuery】表单验证插件——validate
- jquery中使用setTimeout()
- jquery背景backgroundPosition插件
- 关于用jquery判断图片是否加载成功
- jquery.validate ajax提交
- jQuery全屏背景图片导航
- 用CSS和jQuery制作简单的下拉框
- Jquery 行选中背景色
- jquery ui 的弹出窗体 dialog 高度会产生变化
- 如何不刷新页面而刷新页面中某个文件
- 培训第四节课笔记(DOM,JQUERY)
- jQuery 1.x and 2.x , which is better?
- 微信公众账号开发利器 - jQuery WeUI V0.6.0 发布
- jQuery动态五星评分