简体   繁体   English

当数据以JSON格式发送时,使用从php返回的数据

[英]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 我目前是JSON的新手,我认为它可能真的很有用,以下是我用来将JSON对象发送到服务器端php脚本的数据格式

    // 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) 然后是服务器端(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. 问题是,即使我用萤火虫检查了它,Javascript也不会警告任何返回的任何返回值,它表明响应实际上是已发送但未警告。 would like to know where the Javascript error lies 想知道Javascript错误在哪里

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. 调用jQuery.ajax()时, dataType属性是服务器返回的数据的类型,而不是发送给它的数据的类型。

dataType (default: Intelligent Guess (xml, json, script, or html)) dataType(默认值:Intelligent Guess(xml,json,脚本或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 AJAX调用期望的响应本身就是JSON,但您只是输出一个字符串。 jQuery implicitly tries to parse that as JSON, fails, and as a result executes the error callback. jQuery隐式尝试将其解析为JSON,但失败,并因此执行错误回调。

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. 目前,您仅在回显数据,但不是JSON格式。

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: 然后在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. 您无需对该变量进行字符串化,只需传递{ JsonEmailEntity: JsonEmailEntity } ,jQuery就会相应地对其进行转换。

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. 在Firebug中,如果转到“ 控制台”选项卡,然后单击请求,则从此处单击“ 参数”选项卡,然后您可以看到正在发送到服务器的内容。

Change your JavaScript ajax code: 更改您的JavaScript ajax代码:

   // 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. 因为如果您将dataType指定为JSON。 the success function will execute if return type is json. 如果返回类型为json,则将执行成功函数。

Or change your Php code as below: 或如下更改您的PHP代码:

Change: 更改:

echo $email;

to: 至:

echo json_encode($email);

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

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