douqian5553
2017-08-13 07:04
浏览 293
已采纳

通过POST将JSON数据发送到PHP

I'm trying to send the JSON below to a PHP script via the POST method.

{
  "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.

How can I do this?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试通过POST方法将下面的JSON发送到PHP脚本。

  {
“operation”:{
“name”:“Nicolas”,
“age”:24,
“sex”:“Male”,
} 
}  
   
 
 

我想做的是处理来自JSON的信息,如名称,年龄,性别,以及打印到PHP响应。 \ n

我该怎么做?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dragon7713 2017-08-13 07:16
    已采纳

    This is how I did with AJAX request to send json data to my 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:

    $postbody = $request->json()->all();
    
    $name = $postbody['name'];
    $age = $postbody['age'];
    $sex = $postbody['sex'];
    
    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题