简体   繁体   English

AngularJS向服务器发出发布请求。 如何使用HTTP.POST在angularjs中添加键和值。 (错误:(POST http:// IP / URL / 403(禁止)))

[英]Angularjs making post request to server. How to add key and value in angularjs using HTTP.POST. (ERROR : (POST http://IP/URL/ 403 (Forbidden)))

I am beginner programming with Angularjs and I want to make post request to my server. 我是Angularjs的初学者编程,我想向服务器发出发布请求。 On the server side I'am waiting data with key value pair. 在服务器端,我正在等待具有键值对的数据。 I would like to know how to add the key and value in the HTTP.post. 我想知道如何在HTTP.post中添加键和值。

Additional info: XAMP & Using CodeIgniter for the API. 附加信息:XAMP和对API使用CodeIgniter。


This is what I have for the controller. 这就是我要给控制器的东西。

// calling our submit function.
    $scope.submitForm = function() {
    // Posting data to php file
        var data = ({
            'username': $scope.username,
            'password': $scope.password,
            'email': $scope.email,
            'fName': $scope.firstName,
            'lName': $scope.lastName,
            'gender': $scope.gender
        });

        var config = {
            headers : {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }

        $http.post('http://IP/URL/ ', data, config)
        .success(function (data, status, headers, config) {
            $scope.PostDataResponse = data;
        })
        .error(function (data, status, header, config) {
            $scope.ResponseDetails = "Data: " + data +
                "<hr />status: " + status +
                "<hr />headers: " + header +
                "<hr />config: " + config;
        });
    };

When I submit the data, this is what I get. 当我提交数据时,这就是我得到的。

CONSOLE ERROR : http://i.stack.imgur.com/Qh8Ci.jpg 控制台错误: http//i.stack.imgur.com/Qh8Ci.jpg

Network Preview information : " http://i.stack.imgur.com/oKtM4.jpg 网络预览信息:“ http://i.stack.imgur.com/oKtM4.jpg

The solution to my problem was 解决我的问题的方法是

1) Modify the code to this 1)修改此代码

function ($scope, $http, $stateParams, $httpParamSerializerJQLike) {

$scope.data = {}
  // calling our submit function.
    $scope.submitForm = function() {
    // Posting data to php file


      $http({
    url: url,
    method: 'POST',
    data: $httpParamSerializerJQLike($scope.data),//
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
        'X-API-KEY' : '123456'
    }
  }) .success(function (data, status, headers, config) {
            $scope.PostDataResponse = data;
        })
        .error(function (data, status, header, config) {
            $scope.ResponseDetails = "Data: " + data +
                "<hr />status: " + status +
                "<hr />headers: " + header +
                "<hr />config: " + config;
        });
    };

I modified the above code, thanks to this link/post that Mike provided to me. 由于Mike提供给我的链接/帖子,我修改了上面的代码。 ( How do I POST urlencoded form data with $http in AngularJS? ) 如何在AngularJS中使用$ http发布urlencode表单数据?

2) I Change the headers to this 2)我将标题更改为此

headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'X-API-KEY' : 'PASSWORD' })

3) Implement this to the rest_controller in the API (Server side) 3)在API(服务器端)中的rest_controller上实现

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With,   
Content-Type, Accept, Access-Control-Request-Method");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
$method = $_SERVER['REQUEST_METHOD'];
if($method == "OPTIONS") {
die();
}

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

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