2

Hello i use same script for Laravel 4.2 and Laravel 5.1 and problem is for Laravel 4.2 work perfectly, but on Laravel 5.1 i can't understand why it's return bad result

Problem is why I got $request->ajax() false for Laravel 5.1?

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

}

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

            }   
        }
    });
});

upload-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. If you test for laravel 4.2 this code need change from "{!! .. !!}" to "{{ .. }}"

5
  • 2
    try $request->wantsJson() instead, it relies on the standard Accepts header instead of the non-standard X-Requested-With header. This may be a duplicate of this question Commented Nov 19, 2015 at 20:26
  • Are other parts of your controller working? Commented Nov 19, 2015 at 20:26
  • others methods is simple methods not ajax Commented Nov 19, 2015 at 20:34
  • @watcher i think u don't understand problem.. i got not ajax request, after press image edit, i got redirect in route, but not ajax request.. Commented Nov 19, 2015 at 20:39
  • @J.Kol, try watcher's method. He is correct and he already understood the problem. Commented Nov 19, 2015 at 20:53

1 Answer 1

2

I do not think this problem is caused by Laravel version. Laravel sources show, that ajax() call is propagated to Symfony's request component. And that source changed good 5 years back.

You should trace if X-Requested-With header is sent to your application in both cases. You can also set breakpoint to Symfony\Component\HttpFoundation\Request::isXmlHttpRequest() and see what you have in headers.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.