PHP returns the array to JS problem

Source: Internet
Author: User
PHP End loadmenu.php
$page = $_post ["page"];
$xml = simplexml_load_file (' Menu.xml ');
$root = $xml $page;
$arrJson =array ();
$arrJson ["Key"]= $root->attributes ()->key;//get the name of the menu key
echo Json_encode ($arrJson);
?>
JS End
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);


The last message that pops up is: {"key": {"0": "\u8868\u5355\u7ba1\u7406}}
Why not {"key": "Menu Name"}?


Reply to discussion (solution)

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

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:param ...
Also no, popup is [object Object]

Datatype:jason,

Datatype:json,

Your response is the string bar. Not a JSON object

Datatype:jason,

Datatype:json,

Your response is the string bar. Not a JSON object

PHP End loadmenu.php
$page = $_post ["page"];
$xml = simplexml_load_file (' Menu.xml ');
$root = $xml $page;
$arrJson =array ();
$arrJson ["Key"]= $root->attributes ()->key;//get the name of the menu key
echo Json_encode ($arrJson);
?>
Is that a problem?

What I'm more curious about is:
1, $root->attributes ()->key no error?
2. The popup message is: {"key": {"0": "\u8868\u5355\u7ba1\u7406}}
Where did this 0 come from?

You better put out a menu.xml, test it.

Reference 5 Floor Ohmygirl's reply: Datatype:jason,

Datatype:json,

Your response is the string bar. Not a JSON object

PHP End loadmenu.php
$page = $_post ["page"];
$xml = simplexml_load_file (' Menu.xml ');
$root ...


Header ("content-type:application/json;charset= .......");

var params = {' page ': Nowweb}; This nowweb is defined, if not, enclosed in quotation marks.
Check if the data returned from PHP is normal.

What I'm more curious about is:
1, $root->attributes ()->key no error?
2. The popup message is: {"key": {"0": "\u8868\u5355\u7ba1\u7406}}
Where did this 0 come from?

You better put out a menu.xml, test it.





Gentlemen, is this question a Chinese garbled problem?

var params = {' page ': Nowweb}; This nowweb is defined, if not, enclosed in quotation marks.
Check if the data returned from PHP is normal.
There's no problem with PHP.

Gentlemen, is this question a Chinese garbled problem?

Not garbled. is just the utf-8 encoding of Chinese in JSON format.

UrlDecode can see the normal output in Chinese. But that is not necessary. Because JS processing JSON is also in accordance with the format of Utf-8

Write like this

$page = $_post ["page"]; $xml = simplexml_load_file (' Menu.xml '); if ($root = $xml, $page) {//The arguments that may be passed are invalid  $arrJson = Array ();  $arrJson ["key"] = current ($root->attributes ()->key);//Get the name of the menu key  Echo json_encode ($arrJson);}
function Process_menu (nowweb) {  var url = "loadmenu.php";  var params = {' page ': Nowweb};  $.post (url,params,function (data) {    menu_back (data);  }, ' json ');} function Menu_back (response) {  alert (response.key);} Process_menu (' form '); Test it.

You first direct access to PHP to see if the output is not a problem, if there is a problem, is the PHP side of the problem

function Menu_back (response)
{
Response = eval (' (' +response+ ') ')
alert (response);

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.