简体   繁体   English

在Angularjs $ http.post中向webapi发出问题

[英]issue in Angularjs $http.post to webapi

I am reading angularjs $http.post shortcut method on the following site http://docs.angularjs.org/api/ng .$http#methods_post 我在以下网站上阅读angularjs $ http.post快捷方法http://docs.angularjs.org/api/ng。$ http#methods_post

it accept following 3 parameters. 它接受以下3个参数。

post(url, data, config)

Currently i am facing an issue how to pass data from my view. 目前我正面临着如何从我的视图传递数据的问题。 Here is my view code which ask user to enter ID and ContactNumber. 这是我的视图代码,要求用户输入ID和ContactNumber。

<div data-ng-controller="postreq">
    <form>
    RequestID:<input type="text" data-ng-model="request.Id"/>
    ContactNo:<input type="text" data-ng-model="request.newcontact"/>
    <button data-ng-click="add()">Add</button>

    </form>
</div>

Here what i am trying in my Angularjs controller and it is not working .. no idea how to get the entered values of request.Id and request.newcontact. 这里我在我的Angularjs控制器中尝试它并没有工作..不知道如何获取request.Id和request.newcontact的输入值。

function postreq($scope, $http) {

    $scope.add = function ()
    {
     $http.post(
    '/api/request',
    JSON.stringify(**?????? here i am confused how to pass data**),
    {
        headers: {
            'Content-Type': 'application/json'
        }
    }
).success(function (data) {
    $scope.request= data;
});

}

Here's a baic example of how to do a $http post with Angular: 以下是如何使用Angular执行$ http帖子的基本示例:

 $http({
    cache: false,
    url: "/api/request",
    method: "POST",
    data: {email: "my_login_email@example.com", password: "123456"}
  }).
  success(function (data) ->
    // here the "data" parameter has your response data
  ).
  error(function () ->
    // if you are here something is not going so good
  )

Also keep in mind that Angular doesn't POST data like a normal form (this is pretty important depending on your back-end system, for eg in PHP you won't be able to use $_POST). 另外请记住,Angular不会像普通表单那样POST数据(这非常重要,具体取决于你的后端系统,例如在PHP中你将无法使用$ _POST)。 In order to do that, you'll need a bit more work: 为了做到这一点,你需要做更多的工作:

// your module
myModule = angular.module("myModule ", []);
// you module config
myModule.config (["$httpProvider", function($httpProvider) {
   $httpProvider.defaults.transformRequest = function (data) {
       if data == undefined {
         return data
       }
       return $.param(data)
   }
   $httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8"
}])

Also notice the $.param which is jQuery. 另请注意$ .param是jQuery。 You can find other ways to do this if you don't use jquery. 如果不使用jquery,可以找到其他方法。

$http does a few things internally so that we, as application developers, don't have to think about them. $ http在内部做了一些事情,因此作为应用程序开发人员,我们不必考虑它们。

  1. Does a JSON.stringify if the content needs it 如果内容需要JSON.stringify吗?
  2. Adds appropriate headers 添加适当的标头
  3. Allows for interception before/after request 允许在请求之前/之后进行拦截
  4. Handles caching of responses (not that relevant to your question, but good to know) 处理响应的缓存(与您的问题无关,但很高兴知道)

So, this means that when using $http , we don't need to explicitly do these things. 所以,这意味着当使用$http ,我们不需要明确地做这些事情。 You can change your code to: 您可以将代码更改为:

$http.post('/api/request', $scope.request);

This will achieve a everything that your question requires. 这将实现您的问题所需的一切。 BTW, this will return a promise with success and error callback methods. 顺便说一句,这将返回一个successerror回调方法的promise These can be used to handle the response. 这些可用于处理响应。

You can use this syntax: 您可以使用以下语法:

$http.post(
'/api/request',
$scope.request,
{
    headers: {
        'Content-Type': 'application/json'
    }
});

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

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