Php 如何使用jQuery通过$.get函数显示数据?

Php 如何使用jQuery通过$.get函数显示数据?,php,jquery,getjson,Php,Jquery,Getjson,我有这个问题,我已经挣扎了一段时间,现在我需要帮助: 我有以下数组 array(2) { [0]=> object(stdClass)#4 (4) { ["id"]=> string(1) "1" ["idMake"]=> string(1) "1" ["modelName"]=> string(6) "Legend" ["modelYear"]=> string(4) "1986" }

我有这个问题,我已经挣扎了一段时间,现在我需要帮助: 我有以下数组

array(2) {
  [0]=>
  object(stdClass)#4 (4) {
    ["id"]=>
    string(1) "1"
    ["idMake"]=>
    string(1) "1"
    ["modelName"]=>
    string(6) "Legend"
    ["modelYear"]=>
    string(4) "1986"
  }
  [1]=>
  object(stdClass)#5 (4) {
    ["id"]=>
    string(1) "2"
    ["idMake"]=>
    string(1) "1"
    ["modelName"]=>
    string(3) "MDX"
    ["modelYear"]=>
    string(4) "2000"
  }
}
如何通过jQuery使用
$.get()
函数来实现以下功能:

id   Model      Year
  1    Legend     1986
  2    MDX        2000
我已经尝试了以下方法:

process.php文件:

<?php
require 'DataLayer.class.php';
$dl = new DataLayer();

//get car make models
$models = $dl->getCarModels($id);
if(isset($models)){
    echo json_encode(json_encode($models));
}
else{
    echo 'failed';
}
?>
单击某些链接时调用Javascript函数

function getCarModels(id, make){
    $.get(process.php, function(data){       
        var models = $.parseJSON(data); 
        for(var model in models.model.modelName){ // I got stuck here

        } 
    });
} 
希望这对某人有意义。谢谢

只要在这个链接上找到一些类似的问题:我正在尝试


正如我从一开始就说过的,我的问题与在这个链接上发布的问题非常相似。我唯一的问题是,我真的不知道为什么他们使用json_encode()函数的次数是我为使其正常工作所做的两倍,就是删除其中一个json_encode()函数

如果您能解释一下为什么我只使用jsan_encode()函数来获得我想要的结果,而在大多数教程中,在输出数据之前已经有两次了,我将不胜感激


再次感谢。

您不需要在jQuery ajax中使用parseJSON,jQuery已经可以处理了。您的数据是一个对象数组。下面是使用
$循环的示例。每个

您的
getCarModels()
函数未设置为发送数据

function getCarModels(id, make){

     var dataToServer={ id: id, make: make};/* need to match these keys to $_GET keys in php*/
    $.get(process.php, dataToServer, function(data){       
        $.each( data, function(i, item){
            $('body').append('<p>'+item.modelName+'</p>');
        })
    });
}

您不需要在jqueryajax中使用parseJSON,jQuery已经可以处理这个问题了。您的数据是一个对象数组。下面是使用
$循环的示例。每个

您的
getCarModels()
函数未设置为发送数据

function getCarModels(id, make){

     var dataToServer={ id: id, make: make};/* need to match these keys to $_GET keys in php*/
    $.get(process.php, dataToServer, function(data){       
        $.each( data, function(i, item){
            $('body').append('<p>'+item.modelName+'</p>');
        })
    });
}

使用firebug查看
models
的内容以及从服务器获取响应的内容可能会有所帮助。感谢您的建议:我有以下字符串:[{“id”:“1”,“idMake”:“1”,“modelName”:“Legend”,“modelYear”:“1986”},{“id”:“2”,“idMake”:“1”,“modelName”:“MDX”,“modelYear”:“2000”}]我不太确定如何处理这种类型的字符串而不是arrayoi,oi返回的是一个json数组,您可以使用Jquey$。每个方法循环遍历它。如果您不确定是否使用firebug调试模式来查看@justinWell所建议的情况,那么我使用firebug调试模式得到了一个空响应。非常奇怪…使用firebug查看
模型的内容以及从服务器获取响应的内容可能会有所帮助。感谢您的建议:我有以下字符串:[{“id”:“1”,“idMake”:“1”,“modelName”:“1”,“modelName”:“Legend”,“modelYear”:“1986”},{“id”:“2”,“idMake”:“1”,“modelName”:“MDX”,“modelYear”:“2000”}]我不太确定如何处理这种类型的字符串而不是arrayoi,oi返回的是一个json数组,您可以使用Jquey$。每个方法循环遍历它。如果您不确定是否使用firebug调试模式来查看@justinWell所建议的情况,那么我使用firebug调试模式得到了一个空响应。非常奇怪……如果没有解析json,“alert(data)”会给我“[{“id\”:“1\”,“idMake\”:“1\”,“modelName\”:“Legend\”,“modelYear\”:“1986\”,{“id\”:“2\”,“idMake\”:“1\”,“modelName\”:“MDX\”,“modelYear\”:“2000\”,“我想这是因为我在php文件中使用了“json U编码(json\”($models))。我在这个页面上发现了一个非常类似的问题:[堆栈溢出]()。我一直使用未定义的值,就像我使用“$”一样。每个(模型,函数(){alert(models[0]['modelName']);})在ajax中使用
json
作为数据类型,或者使用$.getJSON,如果在php和mke中出现add header application/json问题,请确保在没有解析json的情况下没有任何其他内容,则“alert(data)”给我“[{\'id\”:“1\”,“idMake\:“1\”,“modelName”\“:\“Legend\”,“\“modelYear\”:“1986\”,{\“id\”:“2\”,“idMake\”:“1\”,“modelName\”:“MDX\”,““modelYear\”:“2000\”””,“'我想这是因为我在process.php文件中使用了“json\(json\)编码($models))。我在这个页面上发现了一个非常类似的问题:[堆栈溢出]()。我一直有未定义的值,就像我做的那样,$。每个(models,function(){alert(models[0]['modelName'];})在ajax中使用
json
作为数据类型,或者使用$.getJSON,如果在php和mke中出现问题,请添加头应用程序/json,确保没有任何其他内容
 $id=$_GET['id'];
 //get car make models
 $models = $dl->getCarModels($id);