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
appServicebut are referencingAppService(note the capitalization). Angular's DI is case-sensitive, so these need to match. - Incorrect Request Body Initialization: You're trying to set
NameandDescriptionwhen initializing$scope.addInfos, but at that point,$scope.nameand$scope.descriptionare 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 ofDescription—that will cause the field to be missing from your request body. - Unhandled Response/Error: If you're not properly handling the
$httppromise'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
- Dependency Injection Consistency: We've aligned
appServicein both the injection array and the function parameter to avoid DI errors. - Dynamic Request Body: The
requestBodyis created inside thesubmitFormfunction, so it uses the latest values from$scope.nameand$scope.descriptionwhen the user submits. - 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. - Fixed Typo:
Descriptionis 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.postautomatically sets theContent-Typeheader toapplication/json, but make sure your backend API accepts this content type. - Form Validation: Add basic validation (e.g., check that
$scope.nameand$scope.descriptionaren't empty) before sending the request to avoid sending incomplete data.
内容的提问来源于stack exchange,提问作者WhoAmI
相关产品推荐
相关产品推荐

