准备写api试试,但是ajax一直遇到跨域问题,postman都能正确获取,所以查资料

2021-01-19 05:14

阅读:527

标签:throw   ref   文件   ons   protocol   val   har   tty   -o   

function goupdathyzt(sqid, zt) {

$.ajax({
async: false,
url: "http://localhost:17298/api/aaa/sss",
type: "get",
//必填
contentType: "application/json; charset=utf-8",

data: { "huo_id": 13, "huozhu_id": 7 },//必要
timeout: 5000,

//服务器段返回的对象包含name,data属性. 
success: function (result) {
alert(result.obj.huo_num);
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
});
}

在global中增加如下

 protected void Application_BeginRequest()
        {
            if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
            {
                Response.End();
            }
        }

配置文件中增加

"Access-Control-Allow-Origin" value="*" />
        
        "Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
        
        "Access-Control-Allow-Headers" value="Content-Type" />
        
        "Access-Control-Request-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
        
      

非常感谢这两位博文

https://www.cnblogs.com/amylis_chen/p/5998215.html

https://www.cnblogs.com/stoneniqiu/p/9034535.html

准备写api试试,但是ajax一直遇到跨域问题,postman都能正确获取,所以查资料

标签:throw   ref   文件   ons   protocol   val   har   tty   -o   

原文地址:https://www.cnblogs.com/guhuaxia/p/12160719.html


评论


亲,登录后才可以留言!