通过POST(PHP)发送JSON负载


Sending JSON Payload through POST (PHP)

我有这些数据。我怎么能把它发送到一个特定的URL并读取响应呢。

 {
  "agent": {
    "name": "Minecraft",
    "version": 1
  },
  "username": "mojang account name/email",
  "password": "mojang account password",
}

您可以使用jQuery、使用$.post简写$.ajax

像这样-

$.post(
 "script.php", {
 "agent": {
 "name": "Minecraft",
 "version": 1
},
 "username": "mojang account name/email",
 "password": "mojang account password",
},
function(dataReturned){ /* do something with data returned from the script */ }
);

您可以使用AJAX发送如下

  • url-将数据发送到何处
  • data-您想要发送的内容
  • error-万一ajax调用出现错误
  • dataType-您将收到什么样的数据
  • success-收到服务器响应后要做什么
  • type-请求类型

 $.ajax({
       url: 'http://example.com/sample/get_respose.php',
       data: {
           "agent": {
              "name": "Minecraft",
               "version": 1
            },
            "username": "mojang account name/email",
            "password": "mojang account password"
            },
       error: function() {
          $('#info').html('<p>An error has occurred</p>');
       },
       dataType: 'jsonp',
       success: function(data) {
          // use your response data
       },
       type: 'GET'
    });