PHP:AJAX的POST请求作出回应 [英] PHP: respond to AJAX post request

查看:242
本文介绍了PHP:AJAX的POST请求作出回应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的角度通过GET请求来检索JSON数据。我想邮寄类似的更新JSON文件,并通过发送回更新的json PHP回应。问题是,该页面没有更新时,JSON文件被更新(阿贾克斯)。

HTML

 < HTML NG-应用=对myApp>
    <机身NG控制器=mainController>
< D​​IV>
    < H3>规则与LT; / H3 GT&;
    < UL>
        <李NG重复=在议事规则> {{rule.rulename}}< /李>
    < / UL>
< / DIV>< D​​IV>
    <标签>新规则:LT; /标签>
    <输入类型=文本NG模型=newRule/>
    <输入类型=按钮NG点击=addRule()VALUE =添加/>
< / DIV>

JavaScript的

  VAR对myApp = angular.module('对myApp',[]);
myApp.controller('mainController',['$范围,$ HTTP',函数($范围,$ scope.getItems =功能(){
        $ http.get(本地主机:8888 / JSON / newRules.json)
       。然后(
           功能(响应){
                //成功回调
                $ scope.rules = response.data;
           },
           功能(响应){
                //失败回调
                的console.log(响应);
           }
        );
    };    $ scope.getItems();
    $ scope.newRule ='';
    $ scope.addRule =功能(){
    $ http.post(本地主机:8888 / JSON / newRules.json,JSON.stringify([{RuleName中:$ scope.newRule}))
           。然后(
               功能(响应){
                    //成功回调
                    $ scope.getItems();
                    $ scope.rules =响应;
                    $ scope.newRule ='';
               },
               功能(响应){
                    //失败回调
                    的console.log(响应);
               }
            );
    };}]);

PHP

 < PHP    $数据=的file_get_contents(newRules.json);
    $数据2 =的file_get_contents(PHP://输入);
    $ POSTDATA = json_de code($的数据);
    $ postData2 = json_de code($数据2);
    $ newArray = array_merge($ POSTDATA,$ postData2);
    $ newPost = json_en code($ newArray);
    的file_put_contents(newRules.json,$ newPost);?>


解决方案

我记得角不会自动添加一个应用程序/ x-WWW的形式urlen codeD 头请求,所以在PHP端你可能还需要取消code中的POST数据是这样的:

  //获取POST数据
$数据=的file_get_contents(PHP://输入);
$ POSTDATA = json_de code($的数据);
//处理数据
// ...
//发送响应
回声json_en code($ responseData);

或者你也可以配置角度发送标题,查看细节的>。

I'm using angular to retrieve json data via get request. I would like to update the json file similarly by post, and have PHP respond by sending back the updated json. The problem is that the page is not updating (ajax) when the json file is updated.

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

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  

    $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:

// 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.

这篇关于PHP:AJAX的POST请求作出回应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆