我对一个php文件进行了ajax调用,该文件将数组编码为json数组/对象。我想要做的是将json响应打印成表格式或数组 div的。我坚持如何处理ajax成功的响应。这是我的ajax ..
<script>
$(document).ready(function(){
$("#adapter").keyup(function()
{
var adapter = $(this).val();
var dataString = 'searchword='+ adapter +'&format=json' ;
if(adapter=='' || adapter < 2 )
{
$("#display3").hide('');
}
else
{
$.ajax({
type: "POST",
url: "ajax/phpfile",
data: dataString,
cache: false,
success: function(data)
{
var myObj = data;
///NOT how to print the result and decode in html or php///
}
});
}return false;
});
});
</script>
这是从服务器返回的json响应。我可以提醒整个json响应,所以我知道它正在ajax方面工作......
{"Result":[{"ptos":{"PTOMasterID":"1","PTOMasterPart":"828B-U6805-L1CX","PTOSeriesUniqueID":"22","PTOPrice":"2715.78","PTOSeries":"82","PTOMounting":"8B","PTOTransmission":"U68","PTOSpeed":"05","PTOShifter":"L","PTOAssemblyID":"1","PTOShaftID":"C","PTOSpecialFeature":"X","PTODate":"2011-11-30 17:28:10"}},{"ptos":{"PTOMasterID":"2","PTOMasterPart":"828B-U6805-L3CX","PTOSeriesUniqueID":"22","PTOPrice":"2715.78","PTOSeries":"82","PTOMounting":"8B","PTOTransmission":"U68","PTOSpeed":"05","PTOShifter":"L","PTOAssemblyID":"3","PTOShaftID":"C","PTOSpecialFeature":"X","PTODate":"2011-11-30 17:28:10"}]}
答案 0 :(得分:1)
$(document).ready(function(){
$("#adapter").keyup(function()
{
var adapter = $(this).val();
var dataString = 'searchword='+ adapter +'&format=json' ;
if(adapter=='' || adapter < 2 )
{
$("#display3").hide('');
}
else
{
$.ajax({
type: "POST",
dataType: "json", //set this to json
url: "ajax/phpfile",
data: dataString,
cache: false,
success: function(data)
{
var myObj = data;
///NOT how to print the result and decode in html or php///
console.log(myObj); //to see the object
}
});
}return false;
});
});
或者你可以像这样使用JSON2.js
JSON.parse(text, reviver)
答案 1 :(得分:0)
如果要从ajax调用返回JSON数据,则需要声明dataType: "JSON"
。然后它只是var pto_id = data.ptos.PTOMasterID
,您可以执行$(object).html(pto_id);
来输入值。希望能回答您正在寻找的问题。
答案 2 :(得分:0)
有趣的问题 - 我在网上发现了这个问题。它应该能够遍历所有对象和对象的所有属性。
http://www.openjs.com/scripts/others/dump_function_php_print_r.php