c# jquery ajax 跨域 Get / Post调用webapi / webservice / wcf

  1. 在webapi/webservice/wcf 项目中添加 Global.asax

  2. 在Application_BeginRequest方法中添加如下代码:

复制代码

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, Authorization");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
        }
    }

复制代码
3. 如果是webservice项目的话,在webconfig->system.web 节点下添加如下节点:

<webServices>
  <protocols>
    <add name="HttpGet" />
    <add name="HttpPost" />
  </protocols>
</webServices>
  1. ajax get方式调用webapi:

复制代码

$.ajax({  //ajax get方式调用webapi
                type: "GET",
                contentType: 'application/json',
                url: 'http://localhost:8082/api/Patient/GetPatientInfoById',
                data: { Id: "4343BF1C-5FD9-4ACE-BD90-26ED29C503C8" },
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                },
                error: function (xhr) {
                    console.log(xhr.responseText);
                }
            })

复制代码
5. ajax POST方式调用webapi

复制代码

$.ajax({
                type: "POST",
                contentType: 'application/json',
                url: 'http://localhost:8082/api/Patient/SavePatient',
                data: JSON.stringify(model),
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                },
                error: function (xhr) {
                    console.log(xhr.responseText);
                }
            })

复制代码
6. ajax get 调用webservice

复制代码

$.ajax({      //ajax get 调用webservice
                type: "GET",
                contentType: 'application/x-www-form-urlencoded',
                url: 'http://localhost:8003/PatientService.asmx/GetPatientInfoByEmpiId',
                data: { empiId: "4343BF1C-5FD9-4ACE-BD90-26ED29C503C8" },        //encodeURI
                dataType: 'xml',
                success: function (data) {
                    console.log(data);
                },
                error: function (xhr) {
                    console.log(xhr.responseText);
                }
            })

复制代码
7. ajax post 调用webservice

复制代码

$.ajax({      //ajax post 调用webservice
                type: "POST",
                contentType: 'application/x-www-form-urlencoded',
                url: 'http://localhost:8003/PatientService.asmx/SavePatient',
                data: { patientInfoString: JSON.stringify(model) },        //encodeURI
                dataType: 'xml',
                success: function (data) {
                    console.log(data);
                },
                error: function (xhr) {
                    console.log(xhr.responseText);
                }
            })

复制代码

  1. ajax get 调用wcf

复制代码

$.ajax({  //ajax get 调用wcf
                type: "GET",
                url: "http://localhost:36634/PatientService.svc/GetPatientInfoByEmpiId?empiId=209530010920",
                //data: JSON.stringify({ empiId: model.EmpiId }),
                contentType: "application/json",
                dataType: "json",
                processData: true,
                success: function (data, status, jqXHR) {
            console.log(data);
                },
                error: function (xhr,data,b) {
                    console.log(xhr.responseText);
                }
            });

复制代码

  1. ajax post 调用wcf

复制代码

$.ajax({   //ajax post 调用wcf
                type: "POST",
                url: "http://localhost:8081/PatientService.svc/SavePatient",
                data: JSON.stringify({ patientInfoString: JSON.stringify(model) }),
                contentType: "application/json",
                dataType: "json",
                success: function (data, status, jqXHR) {
                    var result = JSON.parse(data.d);
            console.log(data);
                },
                error: function (xhr) {
                    console.log(xhr.responseText);
                }
            });
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值