10

enter image description hereI am new to "AJAX" and I have been trying to send a request "ONSELECT" using "AJAX" and receive a "JSON" response in "laravel 5".

Here is my View

<select>
<option data-id="a" value="a">a</option>
<option data-id="b" value="b">b</option>
<option data-id="c" value="c">c</option>
</select>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

<script type="text/javascript">
$('select').change(function(){
var data = $(this).children('option:selected').data('id');

$.ajax({
    type    :"POST",
    url     :"http://localhost/laravel/public/form-data",
    dataType:"html",
    data    :{ data1:data },

    success :function(response)
    alert("thank u");
    }),
});
</script>

Here is my Controller to receive ajax request

public function formdata(){
    $data = Input::get('data1');

    //somecodes

    return Response::json(array(
                    'success' => true,
                    'data'   => $data
                )); 
}

Here is my Route

 Route::post('form-data',array('as'=>'form-data','uses'=>'FormController@formdata'));

I also have tried to change the URL of ajax with just only form-data and {{Url::route('form-data')}}.

3
  • Hello Prasad , What error you are getting ? Commented Feb 19, 2015 at 6:16
  • There are no errors but the code just dosen't work Commented Feb 19, 2015 at 6:17
  • also in laravel 5 the syntax are suppose to change thats the main reason Commented Feb 20, 2015 at 12:57

6 Answers 6

20

Laravel 5 uses csrf token validation for security reasons....try this...

  1. In routes.php

    route post('form-data', 'FormController@postform');
    
  2. In master layout file

    <meta name="csrf-token" content="{{ csrf_token() }}" />
    
  3. var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
    $.ajax({
        url: '/form-data/',
        type: 'POST',
        data: {_token: CSRF_TOKEN},
        dataType: 'JSON',
        success: function (data) {
            console.log(data);
        }
    });
    
Sign up to request clarification or add additional context in comments.

Comments

6

Add error callback to your ajax request to find if an error is thrown,

$.ajax({
  type    :"POST",
  url     :"http://localhost/laravel/public/form-data",
  dataType:"json",
  data    :{ data1:data },
  success :function(response) {
    alert("thank u");
  },
  error: function(e) {
    console.log(e.responseText);
  }
});

its better to use console.log() to see detailed information even if the response is a json string. Try the code and let us know if something is logged to the browser console

4 Comments

still no use but it is also about laravel 5 where some syntax are also changing and was released almost 10 days ago
what response do you get from the controller?
I am trying to get a json response but in this function no response is returned at execution. I just want the success response but in json
Thank you. I was struggling with this part: error: function(e) { console.log(e.responseText); }
3

Your jQuery code has syntax error in success callback, that's why its not making any post request to Laravel, please try below javascript it will work

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<select>
<option data-id="a" value="a">a</option>
<option data-id="b" value="b">b</option>
<option data-id="c" value="c">c</option>
</select>

<script type="text/javascript">
 $(function () {
   	$('select').on('change', function (e) {
 		var data = $(this).children('option:selected').data('id');
	$.ajax({
		type    :"POST",
    	dataType:"json",
		url     :"http://localhost/laravel/public/form-data",
		data    :{ data1:data },
		success :function(response) {
				alert("thank u");
		}
	});
});
})
</script>

In Laravel , you can just return array or object and it will automatically convert it to json response

return ['success' => true, 'data' => $data];

1 Comment

try running php artisan route:list and make sure http://localhost/laravel/public/form-data route is registered, use above code it will work
2

You made error in code ,please write it properly.

$.ajax({
    type    :"POST",
    url     :"http://localhost/laravel/public/form-data",
    dataType:"json",
    data    :{ data1:data },
    success :function(response){
    alert("thank u");
    }
});

Update

I just saw your Returning datatype is json , so use

 dataType:"json",

or

 dataType:"jsonp",

2 Comments

if I add an alert after "var data" it works as alert before ajax but after ajax no response
open Firebug , and open Net tab in it. And edit the question , paste screenshot of giving ajax call .
1

The problem was type should be "GET" instead of "POST" and

route get('form-data', 'FormController@postform');

Thank U every one for your help

1 Comment

Great but changing the method of request just because something isn't working is not a good solution. You may check the console to trace the error for post request if you need to learn. Cheers :)
0

Better if you are sending all form data then use data: $(this).serialize() in ajax, and inside form use {{ csrf_field() }}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.