美文网首页
C# jQuery ajax 跨域 Get/Post请求

C# jQuery ajax 跨域 Get/Post请求

作者: AsaGuo | 来源:发表于2018-07-22 22:24 被阅读144次
  1. Global.asax → 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();
            }
        }
  1. 如果是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);
                }
            })
  1. 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);
                }
            })
  1. 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);
                }
            })
  1. 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);
                }
            });

相关文章

  • 跨域实战解决方案

    一.跨域方案 1.JSONP跨域 (1)前端发起jQuery ajax 的get请求 $.getJSON...

  • C# jQuery ajax 跨域 Get/Post请求

    Global.asax → Application_BeginRequest方法中添加如下代码: 如果是webse...

  • ajax、fetch 跨域携带cookie

    一、ajax 跨域携带cookie 原生ajax请求方式: jquery的post方法请求: 服务器端设置: 二、...

  • 跨域

    ??JSONP只能解决GET请求跨域,不能解决POST请求跨域问题,XHR2可以解决GET,POST方式的请求跨域...

  • node.js处理post请求

    注意:浏览器只能发送get请求,那如何发送post请求呢?发送post请求可以手写ajax请求,但是有跨域问题!所...

  • JavaScript学习笔记(三十三)-- jQuery(下)

    jQuery 今天我们继续来聊 jQuery 发送 ajax 请求 发送 get 请求 发送 post 请求 综合...

  • 前端ajax实现和跨域实现

    下面是用chrome实现ajax请求,并对ajax中get和post请求进行封装,最后对跨域请求的前端处理本文用的...

  • 跨域解决方案

    1.使用Jsonp解决跨域(不推荐,因为只支持get请求,不支持post请求) 1.前端AJAX请求dataTyp...

  • ajax跨域请求

    ajax跨域请求(jsonp) 利用JSONP解决AJAX跨域问题的原理与jQuery解决方案JSONP jQue...

  • 用jQuery实现Ajax

    jQuery提供的Ajax方法 type:类型,“POST”或“GET”,默认为“GET” url:发送请求的地址...

网友评论

      本文标题:C# jQuery ajax 跨域 Get/Post请求

      本文链接:https://www.haomeiwen.com/subject/oexjkftx.html