简体   繁体   中英

using returned data from php when data is sent in JSON format

I am currently a newbie to JSON and i think it could be really usefull below is a data format i use to send a JSON object to a server side php script

    // CREATE JSON OBJECT
 var EmailEntity = { "MailMembers":memberecipients , "email":"me@mail.com" } ;

               // send to php server script
        $.ajax({
        type: "POST",
        url: "engine/send-mail.php",
        dataType: "JSON",
        data: {JsonEmailEntity: JSON.stringify(EmailEntity)},
        success: function(Databack){
        alert(Databack);
         }
            });

Then for the sever-side (PHP)

           // get json element and extract contents
           $Json = $_POST['JsonEmailEntity'];
           $EmailEntities = json_decode($Json,true);

           $email = $EmailEntities['email'];

           echo $email;

the problem is that the Javascript doesnt alert any returned any return value even when i checked it with firebug it showed that the response was actually sent but was not alerted. would like to know where the Javascript error lies

Change:

echo $email;

to:

echo json_encode($email);

The dataType property when calling jQuery.ajax() is the type of the data being returned by the server, not the type of the data being sent to it.

dataType (default: Intelligent Guess (xml, json, script, or html))

Type: String

The type of data that you're expecting back from the server. ...

The jQuery AJAX call is expecting a response that is itself JSON, but you're just outputting a string. jQuery implicitly tries to parse that as JSON, fails, and as a result executes the error callback.

Change this:

echo $email;

into this:

echo json_encode($email);

And it should work. At the moment you're only echoing the data, but it's not in JSON-format.

Addition:

For future reference, you can also do this:

$email['email'] = $EmailEntities['email']; //or "some@email.com";
$email['username'] = "some_user";

echo json_encode($email);

and then in Javascript:

success: function(Databack){
    alert("Your username is " + Databack.username + " and your email is " + Databack.email);
}

I would suspect that it may be related to this line

{JsonEmailEntity: JSON.stringify(EmailEntity)},

You do not need to stringify that variable, you can simply pass { JsonEmailEntity: JsonEmailEntity } and jQuery will convert it accordingly.

That being said, since you are decoding it on the server side I'm not sure if the error is related to it.

In Firebug, if you go to the console tab and then click on the request, from there click on the Params tab and you can see what is getting sent to the server.

Change your JavaScript ajax code:

   // CREATE JSON OBJECT
       var EmailEntity = { "MailMembers":memberecipients , "email":"me@mail.com" } ;

   // send to php server script
        $.ajax({
        type: "POST",
        url: "engine/send-mail.php",
        data: {JsonEmailEntity: JSON.stringify(EmailEntity)},
        success: function(Databack){
        alert(Databack);
      }
    });

Because if you are specifying dataType as JSON. the success function will execute if return type is json.

Or change your Php code as below:

Change:

echo $email;

to:

echo json_encode($email);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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