从AngularJS发布JSON数据
我是AngularJS新手,正在使用AngularJS开发一个新的应用程序。从AngularJS发布JSON数据,angularjs,cordova,Angularjs,Cordova,我是AngularJS新手,正在使用AngularJS开发一个新的应用程序。 我尝试用AngularJS$http服务器发布一个JSON对象。但是我得到一个空对象作为响应:“{}”Ypu需要在http请求中传递dataObj,如下所示 app.controller("blankCtrl", ['$scope', '$http', '$parse', function($scope, $http, $parse) { $scope.login = function() {
我尝试用AngularJS$http服务器发布一个JSON对象。但是我得到一个空对象作为响应:“{}”Ypu需要在http请求中传递dataObj,如下所示
app.controller("blankCtrl", ['$scope', '$http', '$parse', function($scope, $http, $parse) {
$scope.login = function() {
$scope.spice = 'tooo';
console.log("hiii");
alert($("#loginform").serialize()); //-------mobile=9860292514&password=123456
// $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
// $httpProvider.defaults.headers.post['Content-Type'] = 'application/JSON';
var dataObj = {
mobile: $scope.mobile,
password: $scope.password
};
$http({
method: 'POST',
url: 'http://edudux.com/manage/index.php?/api/login',
data:dataObj,
headers: {
'Content-Type': 'application/json'
}
})
.success(function(data, status, headers, config) {
alert("status" + status); //-----------status200
alert(angular.isObject(JSON)); // --------true
alert(JSON); //-------------[object JSON]
var a = angular.toJson(dataObj);
alert(a); //----------{}
var b = angular.fromJson(dataObj);
alert(b); //------------[object Object]
alert(data); //----------[object Object]
alert(JSON.stringify(dataObj)); //----------{}
console.log(JSON.stringify(dataObj));
})
.error(function(data, status, headers, config) {
alert(status);
});
};
}]);
我认为这会对你有用:
app.controller("blankCtrl",['$scope','$http','$parse',function ($scope,$http,$parse) {
$scope.login = function() {
$scope.spice = 'tooo';
console.log("hiii");
alert($("#loginform").serialize());
// $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
// $httpProvider.defaults.headers.post['Content-Type'] = 'application/JSON';
var dataObj = {
mobile : $scope.mobile,
password : $scope.password
};
var config = {
headers : {
'Content-Type': 'Content-Type':'application/json;'
}
}
$http.post('http://edudux.com/manage/index.php?/api/login', dataObj, config)
.success(function (data, status, headers, config) {
alert(JSON.stringify(data));
})
.error(function (data, status, header, config) {
alert('error');
});
}
}]);
执行
POST
请求的快捷方式
app.controller("blankCtrl", ['$scope', '$http', '$parse','$q', function($scope, $http, $parse,$q) {
$scope.login = function() {
$scope.spice = 'tooo';
console.log("hiii");
alert($("#loginform").serialize()); //-------mobile=9860292516&password=123456
// $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
// $httpProvider.defaults.headers.post['Content-Type'] = 'application/JSON';
var dataObj = {
mobile: $scope.mobile,
password: $scope.password
};
var deferred = $q.defer();
$http({
method: 'POST',
url: 'http://edudux.com/manage/index.php?/api/login',
data: dataObj,
headers: {'Content-Type': 'application/json'}
}).success(function (data, status, headers, config) {
deferred.resolve(data);
console.log(deferred.promise);
alert(data);
}).error(function (data, status, headers, config) {
console.log(data, status, headers, config);
});
};
}]);
文件:
我希望能帮你回答上面的问题 你需要传递数据参数hhtp requests你在哪里传递你的
dataObj
在$http?@GangadharJannu数据:dataObjRead@Errabi Ayoub我已经尝试了上面的代码…我得到的响应是[object object]Yes,因为我不知道你的WS的响应是什么,例如,如果你有一个像类A这样的响应对象{inta;intb},只需将其更改为alert(data.a);我的web服务响应为{“Success”:[“mobile”:“9860292514”,“password”:“123456”,}}}您可以尝试使用alert(data.mobile);或警报(data.password)这应该返回9860292514或123456作为值read@Naresh Kumar这段代码给我错误为{“error”:“mobile field is mandatory”}
$scope.login = function () {
// URL
var reqURL = "http://edudux.com/manage/index.php?/api/login";
// data
var reqData = {
mobile : $scope.mobile,
password : $scope.password
};
$http.post(reqURL, reqData).then(function (response) {
// response from server. after login
console.log(response.data);
});
};