admin管理员组

文章数量:1430692

When process.php responds to an Ajax request made by form.html it replies with "false" or "true [hash value]" (where hash value is the output of a hashing function). In form.html I want to call a different function for the two possible responses but how do I parse the response? For example must I call
var responses = xmlhttp.responseText.split(" ")
Assuming the hashing function never outputs "false" I could use
if(xmlhttp.responseText != "false")
Both these ways seem kind of hacky and inefficent, is there a better way?

When process.php responds to an Ajax request made by form.html it replies with "false" or "true [hash value]" (where hash value is the output of a hashing function). In form.html I want to call a different function for the two possible responses but how do I parse the response? For example must I call
var responses = xmlhttp.responseText.split(" ")
Assuming the hashing function never outputs "false" I could use
if(xmlhttp.responseText != "false")
Both these ways seem kind of hacky and inefficent, is there a better way?

Share Improve this question edited Sep 24, 2013 at 5:17 HIRA THAKUR 17.8k14 gold badges59 silver badges87 bronze badges asked Apr 1, 2013 at 17:51 CeleritasCeleritas 15.1k39 gold badges121 silver badges203 bronze badges 4
  • use !== instead of != – Janus Troelsen Commented Apr 1, 2013 at 17:54
  • Can you change output from process.php? If yes, you can return JSON and use it later in your function. – dikirill Commented Apr 1, 2013 at 18:10
  • @dikirill yes I can change the output. I don't know JSON. Could you explain why it would be useful to learn in this case? – Celeritas Commented Apr 1, 2013 at 18:16
  • So, you can easily split your logic (true/false) and also send the data (hash) back to JS function. Steffen showed you an jQuery example below. Take a look into JSON.parse() function. – dikirill Commented Apr 1, 2013 at 18:38
Add a ment  | 

2 Answers 2

Reset to default 4

You could do the following in your PHP Code:

$returnValue['ValueA'] = "a value";
$returnValue['ValueB'] = "another value";

echo json_encode($returnValue);

in your JavaScript Code (JQuery is used here):

$.ajax({
    type: "GET",
    dataType: "json",
    url: "./myphpfile.php",
    data: "parameter=parametervalue",
    success: function(data){
        printresult(data);
    }
});

function printresult(data)
{
    alert(data['ValueA']);   
    alert(data['ValueB']);
}

Is this helping you?

I've had a similar situation, here is my solution using basic Javascript.

First on the PHP side, I can have one of four outes (PASS or FAIL on an INSert or UPDate), so my response to AJAX carries those outes upfront:

[...]  
$echoStr = 'PASS/INS/Adding ID Succeeded.';   // INSert successful  
[...]  
$echoStr = 'FAIL/INS/Adding ID Failed';       // INSert failed  
[...]  
$echoStr = 'PASS/UPD/Updating D Succeeded.';  // UPDate successful  
[...]  
$echoStr = 'FAIL/UPD/Updating ID Failed';     // UPDate failed  
[...]  
echo $echoStr; return;                        // Reply to AJAX request.

On the Javascript side (ajax1 is my AJAX obj), I split the response string into three ponents and process accordingly:

[...]  
ajax1.onreadystatechange = function() { 

  if (ajax1.readyState == 4 && ajax1.status == 200) {  

    response = ajax1.responseText;     // PASS or FAIL, INS or UPD, free form text alert

    passFail = response.substr(0,4);   // PASS or FAIL

    insUPD   = response.substr(5,3);   // INS or UPD

    usrMsg   = response.substr(9);     // Free form alert text

    if (passFail == 'PASS' && insUPD == 'INS') {

       // do what you need to do here
    }

    alert(usrMsg);
  }                 //  if (ajax1.readyState == 4 && ajax1.status == 200) {  
}                   //  ajax1.onreadystatechange = function() {  
[...]  

本文标签: phpGet multiple data from an Ajax responseStack Overflow