【问题标题】:laravel 5.1 Request::ajax return falselaravel 5.1 请求::ajax 返回 false
【发布时间】:2016-02-22 04:05:11
【问题描述】:

您好,我对 Laravel 4.2Laravel 5.1 使用相同的脚本,问题出在 Laravel 4.2 完美运行,但在 Laravel 5.1 上我不明白为什么它返回错误结果

问题是为什么我在 Laravel 5.1 中得到了$request->ajax() false?

routes.php

Route::post('/upload-image', [
    'as' => 'upload-image-post',
    'uses' => 'PageController@profileImage'
]);

PageController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class PageController extends Controller
{

    public function profileImage(Request $request) 
    {
        //here is problem
        //result good: (Laravel 4.2 shows true)
        //result bad: (Laravel 5.1 shows false)
        var_dump($request->ajax());
    }

}

上传-image.blade.php (js)

<script>
jQuery(document).ready(function($) {
    $('#upload_image').click(function(event) {
        $('#image').trigger('click');
    });

    $('#image').change(function(event) {
        /* Act on the event */
        $('#imageform').submit();
    });

    $('#imageform').ajaxForm({      
        beforeSubmit: function() {  
        },
        success: function(msg) {
        },
        error: function(request, status, error) {

        },
        complete: function(xhr) {
            if(xhr.status != 401) {
                $('#image').val('');
                result = xhr.responseText;
                result = $.parseJSON(result);

                if( !$.isEmptyObject(result.file_base_url) ) {
                    var img = new Image();
                    img.onload = function() {
                        $('#register_profile_photo').attr('src', img.src);
                        $('#register_profile_photo').attr('alt', result.image_alt);
                        //spinner.stop();
                        $('#upload-image-error').text('');  
                    }
                    img.src = result.file_base_url;
                } else {
                    $('#upload-image-error').text(result.image[0]); 
                }

            }   
        }
    });
});

上传-image.blade.php (html)

<form enctype="multipart/form-data" name='imageform' role="form" id="imageform" method="post" action="{!! route('upload-image-post') !!}">
    {!! csrf_field() !!}
    <div style="display:none;">
        <input type="file" name="image" id="image" placeholder="Please choose your image" >
    </div>
</form>

<div class="profile-img">
    <img style="float: right" id="register_profile_photo" src="default.png" alt="Default image">
    <div style="float: right" class="img-edit"><a href="javascript:void(0);" id="upload_image">Edit picture</a></div>
</div>

PS。如果您测试 laravel 4.2,此代码需要从 "{!! .. !!}" 更改为 "{{ .. }}"

【问题讨论】:

  • 尝试$request-&gt;wantsJson(),它依赖于标准的Accepts 标头而不是非标准的X-Requested-With 标头。这可能是 this question 的副本
  • 控制器的其他部分是否正常工作?
  • 其他方法是简单的方法而不是 ajax
  • @watcher 我想你不明白问题..我没有收到 ajax 请求,按图像编辑后,我在路由中重定向,但没有 ajax 请求..
  • @J.Kol,试试看守的方法。他是对的,他已经理解了问题所在。

标签: ajax laravel laravel-4 laravel-5 laravel-5.1


【解决方案1】:

我不认为这个问题是由 Laravel 版本引起的。 Laravel 消息来源显示,ajax() 调用被传播到Symfony's request component。 5 年前,该来源发生了很大变化。

您应该跟踪X-Requested-With 标头是否在这两种情况下都发送到您的应用程序。您还可以将断点设置为Symfony\Component\HttpFoundation\Request::isXmlHttpRequest() 并查看标题中的内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 2016-01-20
    • 2018-05-19
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    相关资源
    最近更新 更多