简体   繁体   English

通过POST将JSON数据发送到PHP

[英]Send JSON data via POST to PHP

I'm trying to send the JSON below to a PHP script via the POST method. 我正在尝试通过POST方法将以下JSON发送到PHP脚本。

{
  "operation": {
    "name": "Nicolas",
    "age": 24,
    "sex": "Male",
  }
}

What I wanna do is handle the information that is coming from the JSON like name, age, sex, and print unto the PHP response. 我想做的是处理来自JSON的信息,例如名称,年龄,性别,并打印到PHP响应中。

How can I do this? 我怎样才能做到这一点?

This is how I did with AJAX request to send json data to my URL: 这是我处理AJAX请求以将json数据发送到我的URL的方式:

var dataToSend = {
                name: "Nicolas",
                age: 24,
                sex: "Male"
            }

            var jsonDataToSend = JSON.stringify(dataToSend);

            $.ajax({
               type:'POST',
               url: "/your-url",
               data: jsonDataToSend,
               success:function(data){

                    console.log("success");
               },
               error: function (data) {

                    console.log("error");
                }
            });

And how to receive this posted data in request handler on PHP side: 以及如何在PHP端的请求处理程序中接收此发布的数据:

$postbody = $request->json()->all();

$name = $postbody['name'];
$age = $postbody['age'];
$sex = $postbody['sex'];

If you want to send json data first encode in using json_encode($data). 如果要发送json数据,请先使用json_encode($ data)进行编码。 In the php response page call json_decode() to get the response. 在php响应页面中,调用json_decode()以获取响应。 I hope it helps. 希望对您有所帮助。

first of all you need to set type: 'POST' , also a form that will transmit the information. 首先,您需要设置type: 'POST' ,这也是一种可以传输信息的表格。

First Step Is To Package Up Data For Sending 第一步是打包要发送的数据

Before we can transport any data we first have to make sure it is in a form that will transmit easily. 在传输任何数据之前,我们首先必须确保其格式易于传输。 Since it is easy to create an object in JavaScript out of just about anything, we can start with a simple object literal. 由于使用JavaScript几乎可以用任何对象创建对象都很容易,因此我们可以从简单的对象文字开始。 We can add our data to it as we see fit and then send it on its way. 我们可以根据需要添加数据,然后按自己的方式发送。

 Create an object using an object literal.
var ourObj = {};

 Create a string member called "data" and give it a string.

 Also create an array of simple object literals for our object.

ourObj.data = "Some Data Points";

ourObj.arPoints = [{'x':1, 'y': 2},{'x': 2.3, 'y': 3.3},{'x': -1, 'y': -4}];

Now For Transmitting This Data In JQuery AJAX 现在用于在JQuery AJAX中传输此数据

We are going to setup a URL to submit our data to (this would be the PHP script URL we would use like in the action portion of a form), the method which will use (in our case “post”), the data (which will be our JavaScript object we just built) and lastly what to do with the response from the server. 我们将设置一个URL来提交我们的数据(这将是我们将在表单的动作部分中使用的PHP脚本URL),将使用(在我们的示例中为“ post”)方法,数据(这将是我们刚刚构建的JavaScript对象),最后是如何处理服务器的响应。

$.ajax({
   url: 'process-data.php',
   type: 'post',
   data: {"points" : JSON.stringify(ourObj)},
   success: function(data) {
        // Do something with data that came back. 
   }
});

check this out it might help - http://www.coderslexicon.com/easy-way-to-post-multiple-javascript-values-to-php-using-jquery-ajax/ 检查一下可能有帮助-http: //www.coderslexicon.com/easy-way-to-post-multiple-javascript-values-to-php-using-jquery-ajax/

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

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