简体   繁体   English

从PHP返回JSON,将JSON转换为javascript数组,从AJAX调用返回数组

[英]Returning JSON from PHP, convert JSON to javascript array, return array from AJAX call

I am working on a project that uses a function I called AjaxRequest which handles all AJAX requests I make. 我正在一个使用名为AjaxRequest的函数的项目中工作,该函数处理我提出的所有AJAX请求。 I have no problems in making the request however getting the request back is the issue and placing it where I want it on my page is becoming stressful. 我在发出请求时没有任何问题,但是将请求取回是问题所在,并将其放置在页面上我想要的地方变得压力很大。

HTML BIT HTML位

<body onLoad="calling();">
<div id="status">Status: </div>
</body>

JAVASCRIPT BIT JAVASCRIPT位

function calling() {
    var answer = ajaxRequest("testing", "test.php", "test=test");

    document.getElementById("status").innerHTML += answer[1];
    document.getElementById("status").innerHTML += " " + answer[3];
}

function ajaxRequest(app, location, credentials) {  
var extras = ""; 
if(credentials === "" || credentials) { 
        extras = "&" + credentials; 
    }

    var ajax = ajaxObj("POST", location);
    ajax.onreadystatechange = function() {
        if(ajaxReturn(ajax) == true) {
            var obj = JSON.parse(ajax.responseText);
            var arrayObj = [];
            for(var i in obj) { arrayObj.push([i, obj[i]]); }
            return arrayObj;
        }
    }
    ajax.send("app=" + app + extras); 
}

there are two other functions running: ajaxObj and ajaxReturn but I excluded those because they is not the problem. 还有两个其他函数正在运行:ajaxObj和ajaxReturn,但我将它们排除在外是因为它们不是问题。 Furthermore, I am trying to make ajaxRequest an efficient function that could be used by more than one application without having to rewrite all the code in more than one location. 此外,我正在尝试使ajaxRequest成为一种高效的函数,该函数可以被多个应用程序使用,而不必在一个以上的位置重写所有代码。 All error handling acquires before the actual use of ajaxRequest. 所有错误处理都在实际使用ajaxRequest之前获取。

PHP BIT PHP位

<?php
if($_POST['app'] == "testing") {
    $hey = array('success' => 1, 'message' => 'Successful');
    echo json_encode($hey);
    exit();
}
?>

I'm using calling as a javascript function that does all error handling, this is just basic for the whole of my project however I try to get the JSON from php and convert it to array and the issue is returning the array into calling. 我正在使用call作为执行所有错误处理的javascript函数,这只是我整个项目的基础,但是我尝试从php获取JSON并将其转换为array,问题是将数组返回给call。 I try to display the information on the page yet nothing works. 我尝试在页面上显示信息,但没有任何效果。

I am not looking to use any JQuery for my project so I would like to exclude the use of it for this piece of code. 我不想在我的项目中使用任何JQuery,因此我想在这段代码中排除使用它。

If you want, you could set the header before sending back the json. 如果需要,可以在发送回json之前设置标头。

header('Content-Type: application/json'); header('Content-Type:application / json');

Usually you don't need it, but it will tell your javascript that it's json, and the array will be transform in a javascript object. 通常您不需要它,但是它将告诉您的javascript它是json,并且该数组将在javascript对象中进行转换。 It work with Jquery, but I assume it'll work without too 它可以与Jquery一起使用,但我认为它也可以使用

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

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