ฉันยังใหม่กับ AngularJS และสำหรับการเริ่มต้นฉันคิดว่าจะพัฒนาแอปพลิเคชันใหม่โดยใช้ AngularJS เท่านั้น
ฉันพยายามโทร AJAX ไปยังฝั่งเซิร์ฟเวอร์โดยใช้$httpจากแอพเชิงมุมของฉัน
สำหรับการส่งพารามิเตอร์ฉันลองต่อไปนี้:
$http({
method: "post",
url: URL,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $.param({username: $scope.userName, password: $scope.password})
}).success(function(result){
console.log(result);
});
นี่คือการทำงาน แต่มันก็ใช้ jQuery $.paramเช่นเดียวกับที่ สำหรับการลบการพึ่งพา jQuery ฉันพยายาม:
data: {username: $scope.userName, password: $scope.password}
แต่สิ่งนี้ดูเหมือนจะล้มเหลว จากนั้นฉันก็ลองparams:
params: {username: $scope.userName, password: $scope.password}
แต่สิ่งนี้ก็ดูเหมือนจะล้มเหลว จากนั้นฉันก็ลองJSON.stringify:
data: JSON.stringify({username: $scope.userName, password: $scope.password})
ฉันพบคำตอบที่เป็นไปได้เหล่านี้สำหรับภารกิจของฉัน แต่ไม่ประสบความสำเร็จ ฉันกำลังทำอะไรผิดหรือเปล่า? ฉันแน่ใจว่า AngularJS จะให้ฟังก์ชันนี้ แต่อย่างไร
$scope.userNameกำหนดหรือไม่? ทำไมคุณไม่ลองdata: data?
$http({method: 'post', url: URL, data: {username: $scope.userName, password: $scope.password}});