简体   繁体   English

jQuery $ .get()返回[object,object]

[英]jquery $.get() returns [object,object]

I am trying to return some vale from controller and using these values in jquery.But jquery function return [object, object]. 我试图从控制器返回一些值,并在jquery中使用这些值。但是jquery函数返回[object,object]。

controller function: 控制器功能:

public function skillsReturnJson() {
    $skilljson = Skill::find()->skill_name;
    return $skilljson;
}

jquery function: jQuery函数:

function addNew() {
     $.get('skills_json', function(data){
           alert(data);
    },'json');

route to controller: 路由到控制器:

 Route::POST( 'skills_json', array( 'uses' => 'MyProfile@skillsReturnJson' ));

Try the Response object for a start and return an array of data. 尝试使用Response对象作为开始并返回数据数组。

public function skillsReturnJson() {
    $skilljson = Skill::find()->skill_name;
    return Response::json(['data', $skilljson->toArray()]);
}

Try using console.log(data) in the place of alert(data) to see what the objects contain. 尝试使用console.log(data)代替alert(data)来查看对象包含的内容。 Basically the data variable is an array of objects. 基本上,数据变量是对象数组。 When you check the console, you'll be able to see what properties those objects contain. 当您检查控制台时,您将能够看到这些对象包含哪些属性。 For example: let's say each object contained a property of 'skill_name', you could access each objects 'skill_name' with data[0].skill_name and data[1].skill_name (because from your post it returned two results 0 for first object and 1 for the second). 例如:假设每个对象都包含“ skill_name”属性,则可以使用data[0].skill_namedata[1].skill_name访问每个对象“ skill_name”(因为从您的帖子中,第一个对象返回两个结果0和1代表第二个)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM