Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working on a project that uses a function I called AjaxRequest which handles all AJAX requests I make. 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

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

JAVASCRIPT BIT

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. 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. All error handling acquires before the actual use of ajaxRequest.

PHP BIT

<?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. 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.

share|improve this question
    
(1) What do you expect the value of answer to be immediately after var answer = ajaxRequest("testing", "test.php", "test=test");? (2) Do you know that the first A in 'AJAX' stands for aynchronous? –  dvijaz Apr 17 at 20:38
    
I expect answer to be an array that I can access all of its information that is returned. I am aware of the asynchronous works of it, however I need a function that does the requests I need and all other bits I can add to the function later. –  Elton Rodriguez Apr 17 at 20:40
    
Where in ajaxRequest is the return statement which would yield that array? –  dvijaz Apr 17 at 20:41
1  
If you're doing the request Synchronously, i.e. not Asynchronously, then you will be able to return a value from your ajaxRequest function. If you're not, then you will need to use a callback function, inside of which you would put your innerHTML assignments, using the server's response. In Synchronous requests, everything halts and waits for the server to respond, which is why a return value from the server is possible. In Asynchronous requests, the script does not halt, so you have to define a callback function to handle the server's response, once it arrives. –  dvijaz Apr 17 at 20:48
1  
(3) What happens if you insert this alert(answer); immediately after var answer = ajaxRequest("testing", "test.php", "test=test");? If I've understood the problem correctly, you will see a popup saying 'undefined'. –  dvijaz Apr 17 at 20:53

1 Answer 1

If you want, you could set the header before sending back the 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. It work with Jquery, but I assume it'll work without too

share|improve this answer
    
no changes detected –  Elton Rodriguez Apr 17 at 20:51
    
I am using headers and they are located in my ajaxObj function that I did not display, want me to display it? –  Elton Rodriguez Apr 17 at 20:51
    
What I'm saying is you should tell it from php to. That way, when it get send to the browser, the browser will know it's json –  Carl Boisvert Apr 23 at 13:11
    
Thank you for your response. I had not seen it sense today. I implemented what you recommended –  Elton Rodriguez Apr 24 at 2:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.