处理$http AngularJS返回数据
Handling $http AngularJS return data
这是AngularJS中的客户端(运行良好):
$scope.ajaxLogin = function(){
var fn = document.getElementById("username").value;
var pw = document.getElementById("password").value;
$http({
url: "myurl",
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: { username: fn, password: pw }
}).success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
$scope.showAlertError();
}).error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
$scope.showAlertError();
});
};
我希望只有在用户和密码匹配的情况下才能成功。服务器端:
if ($result=mysqli_query($con,$sql))
{
$rowcount=mysqli_num_rows($result);
printf($rowcount);
mysqli_free_result($result);
}
在AngularJS之前,我使用了普通的AJAX处理。我以前是这样做的:
function ajax_post(){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "myurl";
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;
if(return_data=="1"){
console.log("this is return data"+return_data);
}else{
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
}
如何将其与AngularJS一起使用?我尝试在成功处理程序中放入类似的代码,但它总是转到else块。
你试过这个吗:
.success(function(data, status, headers, config) {
if(status === 200) {
var return_data = data;
if(return_data==="1"){
location.href = "home.html?username=" + fn;
}
else{
ons.notification.alert({message: 'Login Failed!'});
}
}
}
$scope.ajaxLogin = function(){
var fn = document.getElementById("username").value;
var pw = document.getElementById("password").value;
$http({
url: "myurl",
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: { username: fn, password: pw }
}).success(function(data, status, headers, config) {
//Your server output is received here.
if(data){console.log('username and password matches');}
else {console.log('username and password does not match');}
$scope.showAlertError();
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
$scope.showAlertError();
});
};
相关文章:
- AJAX调用并在Node中获取返回数据
- 角度服务未返回数据
- 统计ajax返回数据中的html元素
- 有没有更好的方法可以测量从Ajax调用返回数据所需的总时间
- Imgur API图像搜索未返回数据
- javascript对象显示为null,但object.properties返回数据
- 如何从ajax调用JQuery返回数据
- 试图从嵌套的回调函数返回数据
- NodeJS FS 不从多个文件返回数据
- ajax函数未在C#中返回数据
- 如何在公共js模块中从web服务返回数据
- 将字符串与 JSON 对象组合以返回数据
- 嵌套异步函数未及时返回数据的问题
- NodeJS JSONStream 以一个长字符串的形式返回数据
- 如何使用 HTML 图像请求将数据发送到服务器,或返回数据作为响应
- 从 Angular 模态服务中的模态主体模板返回数据
- jQuery 每个循环返回数据两次
- $.getJSON 在返回数据后不会运行函数
- 如何访问返回数据中的特定json值
- 工厂返回数据后未设置角度$scope