AngularJS $ http.post没有获得JSON成功响应。而是重定向到REST端点的URL

时间:2022-03-19 20:14:58

Experiencing an issue with AngularJS $http.post. After completing the post requests, the form instead of returning the JSON response, it instead redirects to 127.0.0.1:8000/api/forms/create. The backend response is done with Django Rest Framework. This is a CORs request. The angularJS form is hosted on port 9000 making a call to port 8000. When the server is turned off, instead of returning the error alert, it insteads returns a page not found and the url is once again redirected to 127.0.0.1:8000/api/forms/create. Appreciate any advice.

遇到AngularJS $ http.post的问题。完成发布请求后,表单而不是返回JSON响应,而是重定向到127.0.0.1:8000/api/forms/create。后端响应是使用Django Rest Framework完成的。这是一个CORs请求。 angularJS表单托管在端口9000上,调用端口8000.当服务器关闭时,它不会返回错误警报,而是返回未找到的页面,并且该URL再次被重定向到127.0.0.1:8000/ API /表格/创建。感谢任何建议。

Below is the controller code for Angular script:

下面是Angular脚本的控制器代码:

'use strict';
angular.module('myApp')
  .controller('DemoController', function ($scope, $http, DemoService, Tools) {
  var form_submit = function() {$http.post("127.0.0.1:8000/api/forms/create", {name:$scope.applicant, date:$scope.date, address=$scope.address}).success(function(data){ alert(data); })
  .error(function(error){
    alert('something failed');
  })};
  });

Below is the index.html:

以下是index.html:

    <!DOCTYPE html>
    <head>
    </head>
   <body ng-app="myApp" ng-submit="form_submit()">
    <FORM action="http://127.0.0.1:8000/api/forms/create/" method="post">
   <INPUT type="text" ng-model="applicant" name="name">
    <INPUT type="text" ng-model="date" name="date"> 
    <INPUT type="text" ng-model="address" name="address">
   <INPUT type="submit" value="Send"> 
    </FORM>
    </body>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js"></script>
    <script src="controllers/demo.js"></script>
    </html>

3 个解决方案

#1


0  

Have you tried this:

你试过这个:

<FORM ng-submit="form_submit()">

instead of

代替

<body ng-submit="form_submit()">

It should (hopefully) post your $http in the background

它应该(希望)在后台发布您的$ http

#2


1  

Try to add / slash at the end of POST url (see the APPEND_SLASH setting for reference):

尝试在POST url的末尾添加/删除(请参阅APPEND_SLASH设置以供参考):

$http.post("127.0.0.1:8000/api/forms/create/"...

#3


0  

your form_submit variable in the controller is not a scope variable.

控制器中的form_submit变量不是范围变量。

Instead of using

而不是使用

var form_submit

change it to

改为

$scope.form_submit

I think this will work.

我认为这会奏效。

#1


0  

Have you tried this:

你试过这个:

<FORM ng-submit="form_submit()">

instead of

代替

<body ng-submit="form_submit()">

It should (hopefully) post your $http in the background

它应该(希望)在后台发布您的$ http

#2


1  

Try to add / slash at the end of POST url (see the APPEND_SLASH setting for reference):

尝试在POST url的末尾添加/删除(请参阅APPEND_SLASH设置以供参考):

$http.post("127.0.0.1:8000/api/forms/create/"...

#3


0  

your form_submit variable in the controller is not a scope variable.

控制器中的form_submit变量不是范围变量。

Instead of using

而不是使用

var form_submit

change it to

改为

$scope.form_submit

I think this will work.

我认为这会奏效。