简体   繁体   English

PHP:响应AJAX发布请求

[英]PHP: respond to AJAX post request

I'm using angular to retrieve json data via get request. 我正在使用angular通过get请求检索json数据。 I would like to update the json file similarly by post, and have PHP respond by sending back the updated json. 我想通过邮寄方式同样地更新json文件,并让PHP通过发送回更新后的json做出响应。 The problem is that the page is not updating (ajax) when the json file is updated. 问题在于,当更新json文件时,页面未更新(ajax)。

HTML 的HTML

<html ng-app="myApp">
    <body ng-controller="mainController">
<div>
    <h3>Rules</h3>
    <ul>
        <li ng-repeat="rule in rules">{{rule.rulename}}</li>
    </ul>
</div>

<div>
    <label>New Rules:</label>
    <input type="text" ng-model="newRule" />
    <input type="button" ng-click="addRule()" value="Add" />
</div>

JavaScript 的JavaScript

var myApp = angular.module('myApp', []);
myApp.controller('mainController', ['$scope','$http', function($scope,  $scope.getItems = function() {
        $http.get("localhost:8888/json/newRules.json")
       .then(
           function(response){
                //success callback
                $scope.rules = response.data;
           }, 
           function(response){
                // failure callback
                console.log(response);
           }
        );
    };

    $scope.getItems();
    $scope.newRule = '';
    $scope.addRule = function() {
    $http.post("localhost:8888/json/newRules.json", JSON.stringify([{rulename: $scope.newRule}]))
           .then(
               function(response){
                    //success callback
                    $scope.getItems();
                    $scope.rules = response;    
                    $scope.newRule = '';    
               }, 
               function(response){
                    // failure callback
                    console.log(response);
               }
            );  
    };

}]);

PHP 的PHP

<?php  

    $data = file_get_contents("newRules.json");
    $data2 = file_get_contents("php://input");
    $postData = json_decode($data);
    $postData2 = json_decode($data2);
    $newArray = array_merge($postData, $postData2);
    $newPost = json_encode($newArray);
    file_put_contents("newRules.json", $newPost);

?>

As I remember angular doesn't automatically add an application/x-www-form-urlencoded header to requests, so on the php side you may also need to decode the POST data this way: 我记得angular不会自动向请求添加application/x-www-form-urlencoded标头,因此在php端,您可能还需要以这种方式解码POST数据:

// get the POST data
$data = file_get_contents("php://input");
$postData = json_decode($data);
// process the data
// ...
// send response
echo json_encode($responseData);

Or you can configure angular to send the header, see the details here . 或者,您可以配置angular发送标头,请参见此处的详细信息。

use json_encode function to send response in json 使用json_encode函数在json中发送响应

check http://php.net/manual/en/function.json-encode.php 检查http://php.net/manual/en/function.json-encode.php

in your php script you need to do 在你的PHP脚本中,你需要做

echo json_encode(['id'=>123]);

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

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