HTTP POST使用Angular.js [英] HTTP POST using Angular.js

查看:161
本文介绍了HTTP POST使用Angular.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的情景,我想用Angular.js发出HTTP POST请求。我访问哪些具有只是POST变量参数PHP脚本。什么会从每个脚本返回是一个JSON字符串。通常,在一个HTML表单,你可以像这样的要求:

I'm new to the scene and I want to use Angular.js to make an HTTP POST request. I'm accessing PHP scripts which have parameters that are just POST variables. What gets returned from each script is a JSON string. Normally in an HTML form you can make such a request like:

<form method="post" action="url.php">
<input name="this">
<input name="that">
<input value="Submit">
</form>

根据您的输入和您点击提交后,JSON数据1将返回这样的事情: {code:1}

我的脚本或到承载他们。服务器无法访问

I have no access to the scripts or to the servers that hosts them.

我想知道是否有可能为Angular.js读取JSON数据1,该数据1匹配他们在我的JSON数据2定义是什么,然后将它们输出到我的观点(&LT; pre&GT;数据2&LT; / pre&GT; )。

I was wondering if it's possible for Angular.js to read the JSON data1, match that data1 to what they're defined in my JSON data2, and then output them to my view (<pre>data2</pre>).

例如,如果 {code:1} 检索,我希望我的JSON输出为code#1的值:

For example, if { "code" : 1 } is retrieved, I want my JSON to output the value for code #1:

{ "code" : [
  { 1: "User already logged in." }, 
  { 2: "Wrong parameters, try again."}, 
  { 3: "etc., etc." }
 ] 
};

下面是我的尝试:

<form ng-controller="PhpCtrl" name="f1">
<input type="text" name="name">
<input type="text" name="password">
<pre ng-model="codeStatus">{{codeStatus}}</pre>
<input type="submit" ng-click="add()" value="Submit">
</form>

function PhpCtrl($scope, $http, $templateCache) {
    $scope.method = 'POST';
    $scope.url = 'url.php';
    $scope.codeStatus = "";

    $scope.add = function() {

        $http({
            method: $scope.method, 
            url: $scope.url,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},  
            cache: $templateCache
        }).
        success(function(response) {
            $scope.codeStatus = response.data;
        }).
        error(function(response) {
            $scope.codeStatus = response || "Request failed";
        });
        return false;   
    };
}

所有它发布至今的看法是请求失败哈哈,虽然它的处理HTTP / 1.1 200我知道我还有很长的路要走,但我会AP preciate任何帮助。一旦我弄清楚如何正确的JSON数据1张贴到视图,下一步是匹配,并输出相应的数据2。谢谢你在前进!

All it's posting so far to the view is "Request failed" lol, although it's processing HTTP/1.1 200. I know I still have a ways to go but I would appreciate any help. Once I figure out how to post the proper JSON data1 to the view, the next step is matching and outputting the appropriate data2. Thank you in advance!

推荐答案

Acctually的问题是用PHP你不喜欢取回照常发布的数据,这个​​工作对我来说后端:

Acctually the problem is in the backend with PHP you don't retrieve the posted data like usual, this worked for me:

function PhpCtrl($scope, $http, $templateCache) {
  var method = 'POST';
  var url = 'url.php';
  $scope.codeStatus = "";
  $scope.add = function() {
    var FormData = {
      'name' : document.f1.name.value,
      'password' : document.f1.password.value
    };
    $http({
      method: method,
      url: url,
      data: FormData,
      headers: {'Content-Type': 'application/x-www-form-urlencoded'},
      cache: $templateCache
    }).
    success(function(response) {
        $scope.codeStatus = response.data;
    }).
    error(function(response) {
        $scope.codeStatus = response || "Request failed";
    });
    return false;
  };
}

在PHP文件:

$data = json_decode(file_get_contents("php://input"));
echo $data->name;

希望这有助于。

这篇关于HTTP POST使用Angular.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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