gpt4 book ai didi

Javascript Ajax 可以与 AngularJS 一起使用吗?

转载 作者:行者123 更新时间:2023-11-30 17:03:06 25 4
gpt4 key购买 nike

这里我使用 Ajax XML 创建了一个小登录。它工作正常。

function ajax_post(){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "http://boost.meximas.com/mobile/login.php";
var fn = document.getElementById("username").value;
var ln = document.getElementById("password").value;
var vars = "username="+fn+"&password="+ln;
hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
//document.getElementById("status").innerHTML = return_data;
if(return_data=="1"){

location.href = "home.html?username=" + fn;
}else{
//alert("Login Failed...");
$.mobile.loading( "hide" );
ons.notification.alert({message: 'Login Failed!'});

}

}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(vars); // Actually execute the request

我想将相同的流程与 Angulajs 集成。这将与 AngularJS 一起工作,还是我需要将其重新修改为 AngularJS 进程。我可以找到使用 angularjs 执行 URL Ajax 请求的引用。但找不到我要发送的参数。用户名和密码

在 AngularJS 站点找到这个

// Simple GET request example :
$http.get('/someUrl').
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});

对不起英语

最佳答案

你走对了。您可以像下面的函数一样将参数传递给 Angular $http:

$http({
url: "http://boost.meximas.com/mobile/login.php",
method: "POST",
data: { username: "", password: "" },
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});

关于Javascript Ajax 可以与 AngularJS 一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28506505/

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