sogeniusio's avatar

Laravel Ajax Response outputting Json

Trying to figure out what I'm doing wrong in my implementation.

###Route

Route::get('contact', 'PagesController@getContact'); Route::post('contact', 'PagesController@postContact');

###View

` {{ Form::open(array('id' => 'form-contact', 'data-parsley-validate' => '')) }} ... form inputs ... {{ Form::close() }}

×
            <h4 class="modal-title">{{ $title }}</h4>
        </div>

        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
    </div>
</div>
`

Controller

` public function postContact(Request $request) { ... Some logic Mail::to($data['email'])->queue(new ContactForm($data)); Redis::incr('send.contact');

          //Redirect to contact page
          // return redirect()->back()->with('success', true)->with('message','Your message was successfully sent. Ill be in touch soon');
          $msg = array(
              'status' => 'success',
              'msg' => 'Message sent successfully',
          );
          // return response()->json($msg); 
          return Response::json(array('success' => true, 'payload' => View::make('pages.contact', $msg)));

`

JS

` $(document).ready(function() { $('#form-contact').on(submit, function(e) { e.preventDefault(); var name = $('#name').val(); var company = $('#company').val(); var email = $('#email').val(); var subject = $('#subject').val(); var message = $('#message').val(); var datastring = { name: name, company: company, email: email, subject: subject, message: message }; $.ajax({ type: "POST", url: './contact', data: dataString, dataType: "JSON", success: function(data) { if (data.success) { console.log(data); // notice that we are expecting a json array with success = true and a payload $('#flash-overlay-modal').empty().append(data.payload).modal(); } else { // for debugging alert(data); }, error: function(jqXHR, textStatus, errorThrown) { // What to do if we fail console.log(JSON.stringify(jqXHR)); console.log("AJAX error: " + textStatus + ' : ' + errorThrown + ' : ' + errorThrown); } }); }); });

`

Response

{ "success": true, "payload": {} }

0 likes
1 reply
saraos's avatar

Hola, Pudiste resolver este problema? Me está pasando ahora.

Please or to participate in this conversation.