【问题标题】:Send data from laravel 5.3 to web service with ajax jquery使用ajax jquery将数据从laravel 5.3发送到Web服务
【发布时间】:2017-05-07 20:54:30
【问题描述】:

我想使用 ajax jquery 将数据发送到 laravel 5.3 中的 Web 服务。我的ajax代码是(这个问题中的URL是一个例子):

 $.ajax({
        type: "POST",
        url: "http://199.166.212.50:8080/.../add",
        contentType:'application/json',
        data: {
            "requester":
            {
                "userName": "jac",
                "password": "111"
            },
            "request":
            {
                "userName":userName,
                "password":password,
                "firstName": firstName,
                "lastName": lastName,
                "homeLocationLatLong":
                {
                    "latitude": homeLocationLatLong_latitude,
                    "longitude": homeLocationLatLong_longitude
                },
                "homeLocationText": homeLocationText,
                "homePhoneNumber": homePhoneNumber,
                "cellPhoneNumber": cellPhoneNumber

            }

        },
        dataType: "json",
        success: function (result) {
           console.log(result);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    })

但是当我发送数据时,我看到了这个错误:

XMLHttpRequest cannot load http://199.166.212.50:8080/.../add. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.

我必须做什么?

【问题讨论】:

标签: php jquery ajax web-services laravel-5.3


【解决方案1】:

我有同样的问题。我使用this link 并将Allow-Control-Allow-Origin: * 添加到chrome 浏览器。

【讨论】:

    【解决方案2】:

    这里的问题是端点 URL 不允许访问跨域请求。

    我建议在您的 Laravel 端点上添加此代码以允许跨域请求:

    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
    header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Request-With');
    header('Access-Control-Allow-Credentials: true');
    

    另外,如果你在没有 CSRF 令牌的情况下发布到端点,Laravel 的 CSRF 保护会抛出一个错误。我建议包括令牌。更多信息在这里:https://laravel.com/docs/5.3/csrf#csrf-x-csrf-token

    【讨论】:

    • 我是 laravel 的新手。你能告诉我我必须添加这些行的 laravel 5.3 端点在哪里吗?
    【解决方案3】:

    像这样尝试它会为你工作。

     <script>
      $("#login_info").click(function(){
        var name = $('#username').val();
        var pass = $('#password').val();
        var token_key =  $('input[name=_token]').val();
          $.ajax({
           type: "POST",
           url: '{{url("admin_panel/login/auth")}}',
           data: {
            '_token': token_key,
            'username': name,
            'password': pass
          },
           success: function(data)
           {
            alert('in_sucess');
           }
        })
    });   
    

    【讨论】:

      猜你喜欢
      • 2011-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-13
      相关资源
      最近更新 更多