laravel 5.1 Request :: ajax return false

时间:2015-11-19 20:20:10

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

您好我使用相同的脚本 Laravel 4.2 Laravel 5.1 ,问题出在 Laravel 4.2完美地工作,但在 Laravel 5.1上,我无法理解为什么它会返回错误的结果

问题是我为Laravel 5.1弄错slopedata<-rollapply(zoo(Probes), width=12, function(Probes) { coef(lm(formula=y~Probes$Days, data = Probes))[2] }, by = 6, by.column=TRUE, align="right") 的原因?

routes.php文件

$request->ajax()

PageController.php

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

upload-image.blade.php(js)

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());
    }

}

upload-image.blade.php(html)

<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]); 
                }

            }   
        }
    });
});

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

1 个答案:

答案 0 :(得分:2)

我不认为这个问题是由Laravel版本引起的。 Laravel消息来源显示,ajax()电话会传播到Symfony's request component。而这个消息来源在5年前改变了。

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