1

I'm trying to return a file to be downloaded by the client however I've noticed that anything I put as a return value in my controller doesn't get returned to the client.

Here's the code:

Code in my Controller, I've removed unnecessary lines

public function post_test()
{

    if(Input::get('receive_email'))
    {

        $pdf = new Fpdf('P', 'pt', array(1240, 1754));

        $pdf->AddPage();

        $generated_pdf = $pdf->Output("", "s");


        $body = View::make('test.email_collision')->with($data)->render();
        $message->body($body);
        $message->attach(
            $generated_pdf,
            Sentry::user()->metadata['first_name'] . '_' . Sentry::user()->metadata['last_name'] . '.pdf',
            'application/pdf');
        $message->html(true);
        $message->send();

        if(Message::was_sent())
        {
    // HERE I want to actually return the file to be downloaded
    // return Response::download($pdf->Output("", "i");
            // return $pdf->Output("", "i");
        } else {
            $errors = new Laravel\Messages();
            $errors->add('errors', __('test.error_email_not_sent'));
            return Redirect::back()
                ->with_errors($errors->messages['errors']);
        }

    }

// Trying to not return anything
    // return View::make('collide');
}

Code in my JS file which does the POST

  $("#gEmail").bind('touchstart click', function() {
    $.ajax({
      url: document.URL,
      type: 'POST',
      data: {
        receive_email: "1"
      }
    })
    .fail(function(error) {
      console.error(error);
    });

    uiComplete.hide();

    init();

  });
dan2k3k4
  • 1,388
  • 2
  • 23
  • 48
  • What does $message hold? Also, You can check where the issue is by doing a dd() with the data that's supposed to be returned and then monitoring the response on Developer Tools – Pratheek Rebala Jun 27 '13 at 09:45

1 Answers1

0

You have following JS code

$.ajax({
    url: document.URL,
    type: 'POST',
    data: {
      receive_email: "1"
    }
})
.fail(function(error) {
    console.error(error);
});

But you don't have any success callback in your code, so, whatever you return/echo from the server, it won't be visible/available in the client side. So, you need to add jqXHR.done() and you can just add it like

$.ajax({
    // ...
})
.done(function(data){

})
.fail(function(error) {
    console.error(error);
});

You can also add an .always() after fail(), like (good for debugging)

.always(function(a, textStatus, b) {
     if (textStatus == "success") {
         console.log("Success : " + a);
     } else {
         console.log("Error : " + b);
     }
 });

Also, check this answer, it'll help you in some way, such as setting rersponse type from server.

Community
  • 1
  • 1
The Alpha
  • 143,660
  • 29
  • 287
  • 307