简体   繁体   English

从jquery,php发送和接收json数据

[英]send and receive json data from jquery , php

I working on google sign in with JavaScript , if user signed in with google I will get his basic information as json data and send this data to php script , this script will insert user into my sql database but I don't know how can I retrieve json data which is sending by jquery in php this is my jquery code 我正在使用JavaScript登录到google上,如果用户使用google登录,我将获得他的基本信息作为json数据并将此数据发送到php脚本,此脚本会将用户插入到我的sql数据库中,但我不知道该怎么办检索由jQuery在php中发送的json数据,这是我的jQuery代码

  function update_user_data(response) 
  {
 var dataString = JSON.stringify(response);

alert(dataString);

  $.ajax({
        type: "POST",
        dataType: 'json',
        data: {myData:response},
        url: 'Save.php?id=check_user',
        success: function(msg) {var msgs=JSON.stringify(msg);
        alert(msg);
    //  var array = msg.split(',');
    //  var email =array[0];alert(email);
   //var password = array[1];alert(password);
   //$('#username').val(email);$('#password').val(password);
 // document.getElementById("modal4c").click();
        },
           error: function(msg) {alert(JSON.stringify(msg));
    alert(msg);

        }
  });
 }

you can see that I send the json string to Save.php?id=check_user 您可以看到我将json字符串发送到Save.php?id = check_user

here is th save.php code 这是save.php代码

$obj = $_POST['myData'];

echo $obj['id'];

the json data which I send by javascript is like this 我通过javascript发送的json数据是这样的

"{\"kind\":\"plus#person\",\"etag\":\"\\\"EhMivDE25UywestNG8tqFM2v- 
 A/ExECAGokGQl1532fQP_aRYY\\\"\",\"gender\":\"male\",\"emails\": 
 \"objectType\":\ 
"person\",\"id\":\"23229164823262432640\",\"displayName\":\"mohamad 
 buvaydanı\",\"name\":{\"familyName\":\"buvaydanı\",\"givenName\":\"mohadmad\"},\"url\":\"https://plus.google.com/1128891s326438205624449640\",\"image\"

} }

but the php script reply with "{" message only 但php脚本仅用“ {”消息答复

There is not id in your data. 您的数据中没有id you passing data: {myData:response}, only. 您仅传递data: {myData:response}, your data should be like data: {id:123, myData:response}, and you have to decode it first in php file because it is json format, not array. 您的数据应该像data: {id:123, myData:response},并且您必须首先在php文件中对其进行解码,因为它是json格式,而不是数组。 json_decode($_POST, true);

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

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