Laravel API,请求标题字段在飞行前响应中不允许Access-Control-Allow-Headers进行授权

时间:2018-07-16 09:41:15

标签: jquery laravel api authorization laravel-passport

我有一个使用Laravel Passport(带有密码授权的OAuth2)的Laravel API。当我使用Postman访问端点时,它会正确响应。当我尝试使用jQuery访问时,出现此错误:

  

请求标头字段在飞行前响应中不允许Access-Control-Allow-Headers进行授权

这是我的jQuery代码:

  protected void btnUpload_Click(object sender, EventArgs e)
{
        if (FileUpload1.HasFile)
        {
            try
            {
            FileUpload1.SaveAs("~/Datas/" + FileUpload1.FileName);
            Label1.Text = "File name: " +
                FileUpload1.PostedFile.FileName + "<br>" +
                FileUpload1.PostedFile.ContentLength + " kb<br>" +
                "Content type: " +
                FileUpload1.PostedFile.ContentType;
            string FileName = FileUpload1.FileName;
            string path = string.Concat(Server.MapPath("~/Datas/" + FileUpload1.FileName));

            FileUpload1.PostedFile.SaveAs(path);

            OleDbConnection OleDbcon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");

            OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", OleDbcon);
            OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            //Dt.Clear(); ds.Clear();
            objAdapter1.Fill(ds);
            DataTable Dt = new DataTable();
            Dt = ds.Tables[0];
            DataColumn dc = Dt.Columns.Add("Ser", typeof(Int32));
            int count = 0;
            foreach (DataRow item in Dt.Rows)
            {
                count++;
                item["Ser"] = count;
            }

            TraineeGrid.DataSource = Dt;
            TraineeGrid.DataBind();
        }
             catch (Exception ex)
           {
                 Label1.Text = "ERROR: " + ex.Message.ToString();
           }
       }

        else
        {
            Label1.Text = "You have not specified a file.";
        }
    }

1 个答案:

答案 0 :(得分:1)

您是否检查过Laravel设置?您需要在Laravel端添加CORS。

一些有用的资源:

https://learninglaravel.net/laravel-51-easily-enable-cors

https://github.com/barryvdh/laravel-cors