Reputation: 25
Can't access the json encoded data sent from a php function(Using laravel framework) to an ajax call. I am encoding the database results This is the php function I am using public function newLearner(){
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$student_id = $_POST['student_id'];
if(!empty($first_name) && empty($last_name) && empty($student_id)){
$learner = Learners::where('first_name','=',$first_name)->get();
return json_encode($learner);
}
On the javascript side I have used:
function newLearner() {
var firstname = $('input[name=new_first_name]').val();
var lastname = $('input[name=new_last_name]').val();
var student_id = $('input[name=new_id]').val();
//alert(firstname);
var URL = "/teghlearner/public/admin/newLearner";
var info ={
"firstname":firstname,
"lastname" : lastname,
"student_id" : student_id
};
$.ajax({
type: "post",
url: URL,
data : info,
success: function(result) {
//alert(result['first_name']);
for(var i=0;i<result.length;i++){
var item=result[i];
alert(item['first_name']);
alert(item['last_name']);
}
},
error: function(jqXHR, textStatus, errorThrown) {
$('#data').html(result);
}
});
}
And this is the JSON Returned from the php function
[
{
"id": 24,
"title": "Mr",
"first_name": "Patrick",
"last_name": "Vinc",
"gender": "male",
"email": "[email protected]",
"password": "$2y$10$yCyGBOtX6kF3ghy/k8YuXe4wR9W5hYtTGDkl5trTEd7.s5LntOQ.u",
"phone_type": null,
"phone_number": "0000000000",
"pager_number": "00000000000000",
"address_line_1": "",
"address_line_2": "",
"city": "",
"postal_code": "",
"province": "BC",
"country": null,
"emc_contact": "",
"emc_phone": "000000000000000000",
"emc_relation": "",
"passcode": "",
"locker": "999999",
"combination": "abc567",
"its_username": null,
"its_password": null,
"dictation_number": null,
"emailed": 1,
"signed": 0,
"student_num": "12345634",
"level": "Default",
"persist_code": "",
"activated_at": "2014-08-23 16:04:18",
"program": null,
"school": "",
"service": "",
"undergrad_year": null,
"undergrad_level": null,
"activated": 1,
"activation_code": "",
"undergrad_text": null,
"cpso_num": 0,
"start_date": "2014-08-01",
"end_date": "2014-08-31",
"learner_start_date": "0000-00-00",
"learner_end_date": "0000-00-00",
"vacation_start_date": "0000-00-00",
"vacation_end_date": "0000-00-00",
"physician": "1",
"affiliates": null,
"mask": "",
"mask_fit_month_year": "",
"learner_type": null,
"status": 1,
"last_login": "0000-00-00 00:00:00",
"reset_password_code": "",
"permissions": "",
"created_at": "-0001-11-30 00:00:00",
"updated_at": "2014-08-22 21:27:17"
}
]
How can I get the first_name and last_name from this JSON data in the javascript function? Issues
Upvotes: 1
Views: 263
Reputation: 762
The problem is that the content-type on the response is text/html. This makes jquery think the response is html. jquery does not parse the json in that case. To solve this problem simply remove the json_encode. Your laravel controller should look like this:
$first_name = Input::get('firstname');
$last_name = Input::get('lastname');
$student_id = Input::get('student_id');
if(!empty($first_name) && empty($last_name) && empty($student_id)) {
return Learners::where('first_name','=',$first_name)->get();
}
This works because when you return a collection, Laravel will automatically convert it to json and set the content-type to application/json.
Also note that you should use the Input facade to get the post variables rather than using the _POST superglobal.
Upvotes: 1