Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在另一个MVC项目中使用WebAPI得到的错误是源代码';http://localhost:3038' 不允许_C#_Jquery_Asp.net Web Api - Fatal编程技术网

C# 在另一个MVC项目中使用WebAPI得到的错误是源代码';http://localhost:3038' 不允许

C# 在另一个MVC项目中使用WebAPI得到的错误是源代码';http://localhost:3038' 不允许,c#,jquery,asp.net-web-api,C#,Jquery,Asp.net Web Api,我已经在MVC中创建了一个WebAPI,代码运行良好。我试图在MVC的另一个项目中使用这个Get方法,在调用JQuery函数时,我得到了错误信息 不允许使用源代码 Jquery代码 $(document).ready(function () { $.ajax({ type: "GET", url: "http://localhost:63540/api/emp", data: "{}", contentType: "application/json; charse

我已经在MVC中创建了一个WebAPI,代码运行良好。我试图在MVC的另一个项目中使用这个Get方法,在调用JQuery函数时,我得到了错误信息

不允许使用源代码

Jquery代码

$(document).ready(function () {
$.ajax({
    type: "GET",
    url: "http://localhost:63540/api/emp",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {

        for (var i = 0; i < data.d.length; i++) {
            $("#tbDetails").append("<tr><td>" + data.d[i].E_id + "</td><td>" + data.d[i].E_name + "</td><td>" + data.d[i].Designation + "</td><td>" + data.d[i].DepartmentName + "</td><td>" + data.d[i].E_doj + "</td><td>" + data.d[i].EmpType + "</td></tr>");
        }
    },
    error: function (result) {
        alert("Error");
    }
});
});
$(文档).ready(函数(){
$.ajax({
键入:“获取”,
url:“http://localhost:63540/api/emp",
数据:“{}”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(数据){
对于(变量i=0;i
我已在中为WebAPI启用了cors WebApiConfig.cs文件添加了config.EnableCors()

在API中,我添加了

[启用CORS(来源:,标题:,方法:)]

即使我在API中启用了CORS,也会出现错误,如何解决这个问题?,
谢谢。

尝试以下方法:添加到Global.asax.cs文件中的“Application\u BeginRequest”方法

[启用CORS(来源:*,标题:,方法:)]
我已将Originates更改为astrik,因此它正在工作…

不需要这样:
[EnableCors(Originates:http://localhost:63540,标题:“*”,方法:“*”]
?好的,我会尝试删除这个…我得到了解决方案,我在origins中添加了astrik而不是localhost,但它调用了两次我只调用了一次,第一次没有数据,第二次有数据,所以输出不会显示如何修复这个问题。。。
 HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
 if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
 {
     HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET");// or POST, PUT and etc.. 
     HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
     HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
     HttpContext.Current.Response.End();
 }