gpt4 book ai didi

javascript - 将 javascript post 函数转换为 Angular js

转载 作者:行者123 更新时间:2023-12-03 04:15:37 24 4
gpt4 key购买 nike

我开始学习 Angular JS。我有一个 javascript 函数,它是 post 函数,现在我想将其转换为 Angular js。

这是我的旧 JavaScript 代码:

<script>
function submitFunction() {
var ip_range = "";

var user_name = "wind";

$.post("software.aspx", {"action": "getchartdata","username": user_name},
function(data, status) {

if (status === "success") {
if (data) {
console.log(JSON.stringify(data));

dataParse(data);
} else {
alert("ALERT!");
}

alert(start_date)
}
alert(start_date)


});
}
</script>

我可以从这个旧的 JavaScript 中获取返回结果,它返回 json 结果。下面是我的新 Angular js 函数,它无法工作。我不确定是否需要更改后端代码。

<script>
var app = angular.module('myApp', []);
app.controller('FormCtrl', function ($scope, $http) {

console.log("Preparing..")
$scope.submitForm = function () {
console.log("posting data....");
var username = $scope.form;
$http({
method: 'POST',
url: 'Software.aspx',
data: {username: username},


}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
console.log("Success")
console.log(response)

}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log("fail")

});

};
});
</script>

我希望有人告诉我需要将哪些内容转换为 Angular JS。谢谢。

最佳答案

我发现这个方法可以从 JavaScript 中获得相同的结果。

 $scope.submitForm = function () {
console.log($scope.selected)

console.log(startDate)
$http({
method: 'POST',
url: 'Software.aspx',
data: {
"action": "getchartdata",
"username": $scope.username,

},
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
transformRequest: function (data) {
return $.param(data);
}
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
console.log(response);
dataParse(response.data);


}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log("fail to send")

});

};

需要将标题设置为:

 headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
transformRequest: function (data) {
return $.param(data);
}

只有这样才有效。

关于javascript - 将 javascript post 函数转换为 Angular js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44151125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com