我正在尝试将cordova应用程序与angularjs和离子框架结合使用,以便用户登录和注册。
这是连接到其余api的data.js的代码
app.factory("Data", ['$http', 'toaster',
function ($http, toaster) { // This service connects to our REST API
var serviceBase = 'http://blace.co/task_manager/v1/';
var obj = {};
obj.toast = function (data) {
toaster.pop(data.status, "", data.message, 10000, 'trustedHtml');
}
obj.get = function (q) {
return $http.get(serviceBase + q).then(function (results) {
return results.data;
});
};
obj.post = function (q, object) {
console.log(object);
return $http.post(serviceBase + q, object).then(function (results) {
return results.data;
});
};
obj.put = function (q, object) {
return $http.put(serviceBase + q, object).then(function (results) {
return results.data;
});
};
obj.delete = function (q) {
return $http.delete(serviceBase + q).then(function (results) {
return results.data;
});
};
return obj;
}]);
这是我的控制器authCtrl.js的代码
app.controller('authCtrl', function ($scope, $rootScope, $routeParams, $location, $http, Data) {
//initially set those objects to null to avoid undefined error
$scope.login = {};
$scope.register = {};
$scope.doLogin = function (login) {
Data.post('login').then(function (results) {
Data.toast(results);
if (results.status == "success") {
$location.path('dashboard');
}
});
};
$scope.register = {};
$scope.Register = function (register) {
Data.post('register').then(function (results) {
Data.toast(results);
if (results.status == "success") {
$location.path('dashboard');
}
});
};
$scope.logout = function () {
Data.get('logout').then(function (results) {
Data.toast(results);
$location.path('login');
});
}
});
这是注册部分的剩余api的一部分
$app->post('/register', function() use ($app) {
// check for required params
verifyRequiredParams(array('name', 'email', 'password'));
$response = array();
// reading post params
$name = $app->request->post('name');
$email = $app->request->post('email');
$password = $app->request->post('password');
// validating email address
validateEmail($email);
$db = new DbHandler();
$res = $db->createUser($name, $email, $password);
if ($res == USER_CREATED_SUCCESSFULLY) {
$response["error"] = false;
$response["message"] = "You are successfully registered";
} else if ($res == USER_CREATE_FAILED) {
$response["error"] = true;
$response["message"] = "Oops! An error occurred while registereing";
} else if ($res == USER_ALREADY_EXISTED) {
$response["error"] = true;
$response["message"] = "Sorry, this email already existed";
}
// echo json response
echoRespnse(201, $response);
});
其余的api工作正常,我已经使用Advanced Rest Client
进行了检查 你可以在这里看到它 Elasticsearch index last update time在Internet Explorer中我有这个错误 BAD REQUEST - 由于语法无效,服务器无法处理请求。 (XHR):POST
我认为问题在于
默认情况下,$ http服务将通过将数据序列化为JSON,然后使用内容类型" application / json"发布它来转换传出请求。当我们想要将值作为FORM帖子发布时,我们需要更改序列化算法并使用内容类型发布数据," application / x-www-form-urlencoded"。
但我不知道如何实现这个
任何想法都表示赞赏! 谢谢