Heim  >  Artikel  >  Backend-Entwicklung  >  php 返回数组给JS的有关问题

php 返回数组给JS的有关问题

WBOY
WBOYOriginal
2016-06-13 11:18:00815Durchsuche

php 返回数组给JS的问题
php端loadmenu.php

$page = $_POST ["page"];
$xml = simplexml_load_file('menu.xml'); 
$root=$xml->$page;
$arrJson=array();
$arrJson["key"]=$root->attributes()->key;// 获取菜单的名称KEY
        echo json_encode($arrJson);
?>
JS端
function process_menu()
{
var url = "loadmenu.php" ;
var params = {'page':nowweb};
$.post(url,params,function(data){menu_back(data);});
}

function menu_back(response)
{
alert(response);


最后弹出的信息是: {"key":{"0":"\u8868\u5355\u7ba1\u7406}}
为什么不是 {"key":"菜单名"}?

php
------解决方案--------------------
function process_menu()
{
var url = "loadmenu.php" ;
var params = {'page':nowweb};
$.post(url,params,function(data){menu_back(data);});
}
$.ajax({
   type: "POST",
   url: url,
   data: params ,
   dataType: jason,
   success: function(response){
     menu_back(response);
   }
});
function menu_back(response)
{
alert(response);


------解决方案--------------------
function process_menu()
{
var url = "loadmenu.php" ;
var params = {'page':nowweb};
$.ajax({
   type: "POST",
   url: url,
   data: params ,
   dataType: jason,
   success: function(response){
     menu_back(response);
   }
});
function menu_back(response)
{
alert(response);

------解决方案--------------------
function process_menu()
{
var url = "loadmenu.php" ;
var params = {'page':nowweb};
$.post(url,params,function(data){menu_back(data);});
}
$.ajax({
   type: "POST",
   url: url,
   data: params ,
   dataType: json,
   success: function(response){
     menu_back(response);
   }
});
function menu_back(response)
{
alert(response);

------解决方案--------------------
    dataType: jason,

    dataType: json,

你的response是字符串吧。。不是json对象
------解决方案--------------------
我比较奇怪的是:
1、$root->attributes()->key 不会报错吗?
2、弹出的信息是: {"key":{"0":"\u8868\u5355\u7ba1\u7406}}
这个 0 是从哪里来的?

你最好贴出 menu.xml 的片段,测试一下
------解决方案--------------------
引用:
引用:dataType: jason,

    dataType: json,

你的response是字符串吧。。不是json对象

php端loadmenu.php

$page = $_POST ["page"];
$xml = simplexml_load_file('menu.xml'); 
$root……
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn