6

I want to display a view to the user whenever user clicks on a button using ajax.
This is my code.

// BASE = localhost/project/public/

    $('#button').click(function() {
        $.ajax({
            url: BASE + "user/settings",
            type: 'GET'
        })
        .done(function( data ) {
            console.log( data );
        });
    });  

And my routes.php

Route::get('user/settings', 'UserController@getSettings');

And UserController.php

public function getSettings(){
    return View::make('user.settings');
}

But output is this error:

{"error":{"type":"ErrorException","message":"Undefined offset: 0","file":"H:\\dev    \\xampp\\htdocs\\lchat\\vendor\\laravel\\framework\\src\\Illuminate\\Support    \\Collection.php","line":470}}

EDIT: error was in view. I fixed it.

Problem2: The page which is loading via ajax, itself contains another ajax post request. but it's not sending data via ajax anymore. refreshes the page to send data.

The jquery code:

$('#settings :submit').click(function(e){
    e.preventDefault();
    $.post(BASE +  'settings/save', {
        'userName' : $('#userName').val()
        }, function(data) {
            return 'OK';
    });
});

Problem solved: I used .on to bind event:

$(document).on('click', '#settings :submit', function(e){ ... } );

It's working... Thank everyone.

3
  • 3
    Does your view works outside ajax (on our browser)? http://localhost/project/public/user/settings Commented Oct 30, 2013 at 19:38
  • I am an idiot :| the view itself has this error ! i fixed it. now it's working. anyway it took 1 hour for me to make this work ! Commented Oct 30, 2013 at 19:44
  • 1
    That's how bugs works, you never remember to look where you need to. :) Nice it's working now. Enjoy Laravel! Commented Oct 30, 2013 at 19:48

2 Answers 2

3

Well, for starters, you have an error in your view. The error you're getting is in reference to an array you're trying to access that doesn't actually have the key you're trying to use ($arr[0]).

Once you fix the errors with the view itself, it should work fine. The JavaScript will get the data as HTML, and you can just insert it into wherever you want it to show up.

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

Comments

3

Use string method before view file

return (String) view('Company.allUserAjax');

1 Comment

Thanks, this worked for me. I was trying to pass my view partial to Pusher and it was coming through as a javascript object. Using (String) fixed it.

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.