解决不了code在阿贾克斯,PHP,JSON,Laravel(PHP框架) [英] Can't solve code in Ajax,PHP,Json,Laravel(PHP Framework)

查看:338
本文介绍了解决不了code在阿贾克斯,PHP,JSON,Laravel(PHP框架)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(使用laravel框架)无法访问JSON恩从PHP函数发送codeD数据,以Ajax调用。 我编码数据库结果 这是我使用的PHP函数     公共职能newLearner(){

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);

          } 

在JavaScript端我用的:

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": "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函数中的first_name和last_name从这个JSON数据? 问题

How can I get the first_name and last_name from this JSON data in the javascript function? Issues

推荐答案

问题是内容类型上的反应是text / html的。这使得jQuery的认为反应是HTML。 jQuery的不分析在这种情况下JSON。为了解决这个问题,只要将json_en code。你laravel控制器应该是这样的:

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

这工作,因为当你返回一个集合,Laravel会自动将其转换成JSON并设置内容类型为application / JSON。

This works because when you return a collection, Laravel will automatically convert it to json and set the content-type to application/json.

另外请注意,你应该使用输入门面获得那个职位的变量,而不是使用_POST全局。

Also note that you should use the Input facade to get the post variables rather than using the _POST superglobal.

这篇关于解决不了code在阿贾克斯,PHP,JSON,Laravel(PHP框架)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆