【问题标题】:Ionic Laravel API PUT issue离子 Laravel API PUT 问题
【发布时间】:2014-04-11 16:20:46
【问题描述】:

我正在努力处理以下请求:

var spice = $http.put("http://localhost:8000/api/v1/Spices/", sanitizeSpice(spice, id));

sanitizeSpice 返回类似 {amount: "54", id: "2")的东西

我收到以下错误:

XMLHttpRequest cannot load http://localhost:8000/api/v1/Spices/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. 

我在 Laravel 中设置了以下过滤器(如我在此处找到的其他示例所示):

App::before(function($request)
{
  if (Request::getMethod() == "OPTIONS") {
      $headers = array(
      'Access-Control-Allow-Origin', 'http://localhost',
      'Access-Control-Allow-Methods'=> 'POST, GET, OPTIONS, PUT, DELETE',
      'Access-Control-Allow-Headers'=> 'X-Requested-With, content-type',);
      return Response::make('', 200, $headers);
  }
});


App::after(function($request, $response)
{
    $response->header('Access-Control-Allow-Origin', '*');
    $response->header('Access-Control-Allow-Methods', 'POST,GET,DELETE,PUT,OPTIONS');
});

我做错了什么?我没有收到关于 POST、GET 或 DELETE 的错误。提前致谢!

【问题讨论】:

    标签: angularjs laravel xmlhttprequest


    【解决方案1】:

    如果你用谷歌搜索Access-Control-Allow-Origin,第一个命中是https://developer.mozilla.org/en/docs/HTTP/Access_control_CORS

    您需要在您的 nginx 或 apache2 配置中允许 ajax 调用。

    使用谷歌为他们找到好的配置。 例如。对于 nginx:

    add_header 'Access-Control-Allow-Origin' '*';
    

    【讨论】:

    • 我用谷歌搜索了好几次。允许ajax调用。仍然报错。
    • 你把 add_header 行放在哪里了?尝试发布整个 nginx/apache2 配置。
    猜你喜欢
    • 2019-02-22
    • 2017-02-15
    • 2017-05-13
    • 2019-05-29
    • 2017-03-15
    • 2016-07-22
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多