You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AngularJS POST请求报错:状态值undefined,请求体构造问题求助

Fixing Your Angular POST Request Issues

Hey there! Let's walk through why you're getting an undefined status and how to properly structure your POST request body. From your code snippet, I can spot a few common pitfalls that are likely causing the problem.

Common Issues to Address

  • Dependency Injection Mismatch: You've injected appService but are referencing AppService (note the capitalization). Angular's DI is case-sensitive, so these need to match.
  • Incorrect Request Body Initialization: You're trying to set Name and Description when initializing $scope.addInfos, but at that point, $scope.name and $scope.description are probably still undefined (since the user hasn't entered anything yet). You need to build the request body dynamically when the user submits the form.
  • Typos: Your code has Descriptio (missing the final 'n') instead of Description—that will cause the field to be missing from your request body.
  • Unhandled Response/Error: If you're not properly handling the $http promise's success/error callbacks, you might not be seeing the actual status code or error details.

Corrected Controller Code

Here's a revised version of your controller that fixes these issues:

app.controller('postController', ['$scope', '$http', 'appService', function ($scope, $http, appService) {
  // Initialize your input fields
  $scope.name = '';
  $scope.description = '';

  // Submit function to send the POST request
  $scope.submitForm = function() {
    // Build the request body dynamically with current input values
    const requestBody = {
      Name: $scope.name,
      Description: $scope.description,
      Data: [] // Empty array as required
    };

    // Send the POST request
    $http.post('/your-api-endpoint', requestBody)
      .then(function(response) {
        // Success callback
        console.log('Request successful! Status:', response.status);
        console.log('Response data:', response.data);
        // Handle success (e.g., show a message, reset form)
        $scope.name = '';
        $scope.description = '';
      })
      .catch(function(error) {
        // Error callback
        console.error('Request failed! Status:', error.status);
        console.error('Error details:', error.data);
        // Handle error (e.g., show an error message to the user)
      });
  };
}]);

Key Fixes Explained

  1. Dependency Injection Consistency: We've aligned appService in both the injection array and the function parameter to avoid DI errors.
  2. Dynamic Request Body: The requestBody is created inside the submitForm function, so it uses the latest values from $scope.name and $scope.description when the user submits.
  3. Proper Response Handling: Using .then() and .catch() ensures you can access the response status and data, and handle errors properly—this will eliminate the "undefined status" issue because you're explicitly checking the response/error objects.
  4. Fixed Typo: Description is spelled correctly now, so the field will be included in the request body as expected.

Additional Tips

  • Check Network Tab: Open your browser's dev tools (F12) and go to the Network tab. Look for your POST request—you can verify the request body, status code, and response details here. This is super helpful for debugging.
  • Content-Type: Angular's $http.post automatically sets the Content-Type header to application/json, but make sure your backend API accepts this content type.
  • Form Validation: Add basic validation (e.g., check that $scope.name and $scope.description aren't empty) before sending the request to avoid sending incomplete data.

内容的提问来源于stack exchange,提问作者WhoAmI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:29:14