ajax post 请求报错Response to preflight request doesn't pass access control check: No 'Access-Control-A...

 

jquery ajax跨域请求,webapi webconfig配置

前台代码(放了一部分)

function CheckIn(roomno) {
$.ajax({
url: 'https://www..../RoomAssign',
type: 'post',
async: false,
contentType: 'application/json;charset=utf-8',
dataType: 'json',
data: JSON.stringify({
AccountId: localStorage.getItem("Accountid"),
Address: localStorage.getItem("Address"),
ArrivalDate: localStorage.getItem("Arrivaldate"),
Birthday: localStorage.getItem("Birthday"),
CertificateNo: localStorage.getItem("Certificateno"),
CertificateType: localStorage.getItem("Certificatetype"),
Country: localStorage.getItem("Country"),
})

后台代码:

  public List RoomASSign([FromBody] RoomAssignIn model)
        {
            try
            {
                RoomAssignReq req = new RoomAssignReq();
                req.AccountId = model.Accountid;
                req.RoomNo = model.Roomno;
                req.Rsvno = model.Rsvno;
                RoomAssignRsp rsp = null;
                ErrorInfo error = null;
                List Rd = new List();
                RoomAssign list = new RoomAssign();
                if (db.RoomAssign(req, ref rsp, ref error) == 0)
                {        
                        list.resultcode = rsp.ResultCode;
                        list.description = rsp.Description;                              

                }
                else
                {
                    list.description = error.ErrorMessage;
                }
                Rd.Add(list);
                return Rd;
            }
            catch (Exception ex)
            {
                throw ex;
            }


        }

web配置以下内容


    
    
      
        "HttpSoap"/>
        "HttpPost"/>
        "HttpGet"/>
        "Documentation"/>
      
    
 
 
 
 
   
       
       
      "Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/>   
      "Access-Control-Allow-Headers" value="x-requested-with,content-type"/>   
      "Access-Control-Allow-Origin" value="*" />   
       
     
    
    "MyHttpModule" type="WebServiceDemo.MyHttpModule"/>  
  
    

如果报错:

未能加载类型“WebServiceDemo.MyHttpModule”。去掉

   
    "MyHttpModule" type="WebServiceDemo.MyHttpModule"/>  
  

如果想选定的网站可能跨域访问,修改配置如下:

"Access-Control-Allow-Origin" value="http://www....." />  

 

转载于:https://www.cnblogs.com/wxxf/p/9214674.html

你可能感兴趣的:(ajax post 请求报错Response to preflight request doesn't pass access control check: No 'Access-Control-A...)