4

Basically I'm trying to make a popup using this ajax:

$.ajax({
    type: 'GET'
    url: 'news/read'
    dataType: 'json'
    cache: false
    timeout: 10000
}).done (msg) ->
    $("article#pop-read").empty().html msg.view
    processing = false
    window.history.pushState
        path: msg.url
        , "", msg.url
    false

And I'm returning a view value and it's url with this:

$data = json_encode(array(
        'view' => View::make('layouts.read'),
    'url' => 'news/read/2013/11/24/test-title-seperate-with-dash'
));
return $data;

This all works really well except that I can't get the view value from laravel (it returns Object object in javascript). But it returns nicely if I directly write it like return View::make('layouts.read'). Why is that?

Additionally (don't have to answer, not primary question), the back button on my browser doesn't work when I use pushState, is it a bug?

1 Answer 1

14

You may try this

$data = json_encode(array(
    'view' => (String)View::make('layouts.read'),
    'url' => 'news/read/2013/11/24/test-title-seperate-with-dash'
));
return $data;

Also, you can use

View::make('layouts.read')->render();
View::make('layouts.read')->__toString();

Also, Laravel provides Response::json() method for same reason (instead of json_encode).

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

3 Comments

ok, thanks, all solutions worked. Moving on to pushState problem now.
any idea how to parse the view into the json response? how to give the html of the view to an element in my page using $('#element').html(data.html) ?
Post a question, gimme the link and I'll answer, be specific with details in the question.

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.