• ajax post 请求报错Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' heade


    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> 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<RoomAssign> Rd = new List<RoomAssign>();
                    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配置以下内容

    <system.web>
        <!--提供Web服务访问方式-->
        <webServices>
          <protocols>
            <add name="HttpSoap"/>
            <add name="HttpPost"/>
            <add name="HttpGet"/>
            <add name="Documentation"/>
          </protocols>
        </webServices>
     </system.web>
     
     
     <configuration>
     <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>   
      <modules>  
        <add name="MyHttpModule" type="WebServiceDemo.MyHttpModule"/>  
      </modules>
      </system.webServer>  
    </configuration>

    如果报错:

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

     <modules>  
        <add name="MyHttpModule" type="WebServiceDemo.MyHttpModule"/>  
      </modules>

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

    <add name="Access-Control-Allow-Origin" value="http://www....." />  
  • 相关阅读:
    VS2015 调试中断点突然失效的解决办法、VS调试时关闭调试让浏览器继续保留页面
    Postman调用WebService,包括头验证部分
    C# 正则表达式大全
    Webservice超时问题
    C# DateTime的 ParseExact和 TryParseExact 使用说明
    4、QT分析之调试跟踪系统
    5、QT分析之网络编程
    QIODevice (Qt中所有 I/O devices 的基类,QFile,QBuffer,QTcpSocket等)
    Qt 菜鸟的坑 QAbstractSocket::isValid()
    qt之QAbstractSocket
  • 原文地址:https://www.cnblogs.com/wxxf/p/9214674.html
Copyright © 2020-2023  润新知