gpt4 book ai didi

javascript - 无法解决Ajax、PHP、Json、Laravel(PHP框架)中的代码

转载 作者:行者123 更新时间:2023-11-28 01:02:25 25 4
gpt4 key购买 nike

无法访问从 php 函数(使用 laravel 框架)发送到 ajax 调用的 json 编码数据。我正在对数据库结果进行编码这是我正在使用的php函数 公共(public)函数 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);

}

在 javascript 方面我使用过:

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": "nupur@gmail.com",
"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"
}
]

如何在 javascript 函数中从 JSON 数据中获取名字和姓氏?问题

最佳答案

问题是响应的内容类型是text/html。这使得 jquery 认为响应是 html。在这种情况下,jquery 不会解析 json。要解决这个问题,只需删除 json_encode。你的 Laravel Controller 应该如下所示:

$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();
}

这是有效的,因为当你返回一个集合时,Laravel 会自动将其转换为 json 并将内容类型设置为 application/json。

另请注意,您应该使用输入外观来获取 post 变量,而不是使用 _POST 超全局变量。

关于javascript - 无法解决Ajax、PHP、Json、Laravel(PHP框架)中的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25466891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com