C# jQuery ajax 跨域 Get/Post请求

  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 节点下添加如下节点:

      
        
        
      
    
  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);
                }
            });

你可能感兴趣的:(C# jQuery ajax 跨域 Get/Post请求)