My problem is simple, every time I try to pass a string with special characters, through a
$http.get() query, some special characters are removed.
In my case, a simple email: emma#watson.com, become emmawatson.com in the query header.
Is Angular doing some sort of validation before sending AJAX query ?
Here is my Ajax code:
$http.get(apiUrl,
{
params: {
username: "Emma",
email: "emma#watson.com",
password: "1234"
}
})
.success(function(data) {
//do a simple return of the email on the server
console.log(data);//"emmawatson.com"
});
For http requests, it's strongly recommended to base64 encode your data before sending it.
Try this code and see if it works:
$http.get(apiUrl, {
params: {
username: "Emma",
email: window.btoa("emma#watson.com"),
password: "1234"
}
})
.success(function (data) {
//do a simple return of the email on the server
console.log(data); //"emmawatson.com"
});
Related
I am trying to get access to backend written in GO which in 99% is good (problem does not lay there).
For now I just created simplest call which stay in controller (it will be in service in future) to register new user. Although I hardcoded data which I am passing the response says 403 forbidden. In powerShell shows reason of 403:
RegistrationForm parse - email: , nick:
Validation failed for email - blank
It looks like I am not passing my data correctly because email is blank. Please take a look at my code:
$ctrl.fake_registerSubmit = function() {
$http({
url: 'http://localhost:3000/v1/sign_up',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
method: 'POST',
data: {
email: 'check#onet.pl',
nick: 'borysxoxo',
password: 'admin12312',
password_confirmation: 'admin12312'
}
})
.then(function successCall(response, post) {
console.log('user added');
}, function errorCall(respone) {
console.log('error callback');
console.log(respone);
})
};
This screenshot presents API documentation which I am trying to access:
link
What am I doing wrong? Is there other way to pass data?
You are sending some json data with the wrong Content-Type.
I see 2 options, either change Content-Type in your header to:
'Content-type' : 'application/json'
or transform your payload to:
data: "email=check#onet.pl&nick=borysxoxo&password=admin12312&password_confirmation=admin12312"
i can't access token value from response of data from login api access please help me.
my code is accessing data from url and response is in json format from
my code is
authFactory.login = function( username, password ){
return $http.post('/api/login' , {
username : username,
password : password
})
.then( function (data) {
console.log(data);
AuthToken.setToken(data.token);
return data;
})
}
so "data" has below response in browser.
Object {data: Object, status: 200, config: Object, statusText: "OK", headers: function}
config:Object
data:Object
message:"successfully login!"
success:true
token:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1OGY1ZjZlZDhlMzcyMDA4MDhkMmU4NWQiLCJpYXQiOjE0OTI1MjI1OTR9.EkTtneJvBM47LOvMUMh81XYLI_O5oByf1qjWOxliTcs"
__proto__:Object
headers:function (name)
status:200
statusText:"OK"
__proto__: Object
and if i am accessing url of login api then data is occured as
message:"successfully login!"
success:true
token:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1OGY1ZjZlZDhlMzcyMDA4MDhkMmU4NWQiLCJpYXQiOjE0OTI1MjI1OTR9.EkTtneJvBM47LOvMUMh81XYLI_O5oByf1qjWOxliTcs"
Looks like when you are handling promise, you are calling response as data, try to rename it to response which is more human readable.
after renaming you can find data under response response.data.
authFactory.login = function( username, password ){
var queryParams = {
'username': username,
'password': password
}
$http.post('/api/login' , queryParams)
.then(function (response) {
// replaced data with response, which is more human readable
console.log(response.data);
AuthToken.setToken(response.data.token);
return response.data;
});
}
have you tried data.json().token ?
EDIT
One time happen to me that the I got confused with the response, because I named it data, and the response contained an object with the data.
In that case I had to use: data.data.token.
I hope this is also your case :)
I am using "$http.get" for send request. My API response in browser, it returns what i want. But in Ionic App, it return HTML body tag text.
My Code is:
var params = {
email: 'test#gmail.com',
password: '123'
}
$http.get("https://www.nepalivivah.com/API/index.php/accessapi/loginapi", { params: params }).then(function (data) {
alert(JSON.stringify(data));
}).error(function (data) {
alert(JSON.stringify(data));
});
This is my AngularCode. When i run it in postman, it return valid response. But in Ionic App not Working.
If you want the $http.get, this should work.
var params = {
email: 'test#gmail.com',
password: '123'
}
$http.get("https://www.nepalivivah.com/API/index.php/accessapi/loginapi").then(function (data) {
alert(JSON.stringify(data));
}).error(function (err) {
alert(JSON.stringify(err));
});
However, in this case, what you are trying to do is to login into something. A get request will not work with this. You need to use $http.post at least in this case.
how come its sending data to mongo if I $http.post directly in the controller?
$http.post('/api/users/', vm.newUser).success(function(response){
But using the code below doesn't seems to work, it's not passing any value but generates ID in mongoDB
vm.signupNewUser = function () {
signupService.signupNewUser(vm.newUser).success(function(response){
console.log(vm.newUser);
}).error(function(err){
console.log(err);
})
}
service:
function _signupNewUser(username, password, email, fname, lname){
var data = {
username: username,
password: password,
email: email,
fname: fname,
lname: lname
}
return $http({
method: 'POST',
url: '/api/users',
data: data
})
}
Edit: this seems to fix it, but not sure how is it different from using a direct $http.post and just passing vm.newUser. But using factory I have to pass in vm.newUser.username and etc. I don't think passing the vm like this is ideal but please correct me if I'm wrong.
vm.signupNewUser = function () {
signupService.signupNewUser(vm.newUser.username, vm.newUser.password, vm.newUser.email, vm.newUser.fname, vm.newUser.lname)
.then(function(response){
console.log(response);
console.log(vm.newUser);
});
}
I'm pretty new to backbone and how it works and inherited a bunch of code but I can't solve this at all:
I have a user model:
var User = Backbone.Model.extend({
idAttribute: 'username',
defaults: {
username: "",
email: "",
roles : [],
password: ""
}
});
var Users = Backbone.Collection.extend({
model: User,
initialize: function(args, options) {
if (options && options.dialog) {
this.dialog = options.dialog;
}
},
parse: function(response) {
if (this.dialog) {
this.dialog.populate(response);
}
return response;
},
url: function() {
var segment = AdminUrl + "/users";
return segment;
}
});
Then elsewhere in my view I'm doing:
user.save({username: $newtarget.val()},null);
or
user.save();
The PUT is fired to the correct url but every time its triggered it sends the data
Content-Type application/x-www-form-urlencoded; charset=UTF-8
but my Jersey endpoint accepts application/json
Everywhere I read people are struggling to put urlencoded data but my problem is the otherway around!
Parameters are being send as url params:
username=admin&email=&password=admin&roles%5B%5D=ROLE_USER&roles%5B%5D=ROLE_ADMIN&id=1
===EDIT===
If I force the content type and data:
user.save({}, {data: JSON.stringify(user.attributes),contentType: "application/json"});
The put works fine, which is bizarre.
Backbone.emulateJSON = false;
is true for some reason
From the docs
Turn on emulateJSON to support legacy servers that can’t deal with
direct application/json requests … will encode the body as
application/x-www-form-urlencoded instead and will send the model in a
form param named model.
http://backbonejs.org/docs/backbone.html