Reputation: 478
I've been trying to get to a code behind function to get two variables encrypted to return as querystrings. But I've been unsuccessfull. First time trying Ajax.
So, in code behind I have this method:
[WebMethod]
public static string EncritaDados(string str, string str2)
{
return Verificacoes.EncryptString(str)+";"+ Verificacoes.EncryptString(str2);
}
In my ASP my I have this (implementing facebook login):
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me?fields=name,email', function (response) {
console.log('Successful login for: ' + response.name);
Ajax(response.email, response.name)
});
}
function Ajax(expressao1, expressao2) {
$.ajax({
url: 'login.aspx/EncritaDados',
method: 'post',
contentType:'application/json',
data: '{str: ' + expressao1 + ', str2: ' + expressao2 + '}',
dataType:'json',
success: function (resp) {
var strings = resp.d.split(";");
window.location.href = 'login.aspx?email=' + strings[0] + '&nome=' + strings[1];
},
error: function () { }
})
}
Before trying Ajax it would work, whithout trying to get to the code behind. I had it like this:
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me?fields=name,email', function (response) {
console.log('Successful login for: ' + response.name);
window.location.href = 'login.aspx?email=' + response.email+ '&nome=' + response.name;
});
}
I'm scratching my head right now. Can anyone help me with this? I'd appreciate it.
EDIT: I got it like this:
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me?fields=name,email', function (response) {
console.log('Successful login for: ' + response.name);
Ajax(response.email, response.name);
});
}
function Ajax(expressao1, expressao2) {
var request = { email: expressao1, nome: expressao2 }
$.ajax({
url: 'login.aspx/EncritaDados',
method: 'post',
contentType: 'application/json',
data: JSON.stringify(request),
dataType: 'json',
success: function (resp) {
var strings = resp.d.split(";");
window.location.href = 'login.aspx?email=' + strings[0] + '&nome=' + strings[1];
},
error: function (error) { alert(error.status); }
})
And in code behind:
[WebMethod]
public static string EncritaDados(string email, string nome)
{
return Verificacoes.EncryptString(email) + ";" + Verificacoes.EncryptString(nome);
}
So, basically, I did some minor changes but it wasn't doing what it was supposed to because the data string was bad formed. But I formatted it as suggested with JSON.stringify and it worked.
Upvotes: 1
Views: 2253
Reputation: 1418
You had an error in your javascript. resp.d instead of resp.data:
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me?fields=name,email', function (response) {
console.log('Successful login for: ' + response.name);
Ajax(response.email, response.name)
});
}
function Ajax(expressao1, expressao2) {
var requestObject = { str : expressao1, str2 : expressao2 }
$.ajax({
url: 'login.aspx/EncritaDados',
method: 'post',
contentType:'application/json',
data: JSON.stringify(requestObject),
dataType:'json',
success: function (resp) {
var strings = resp.data.split(";");
window.location.href = 'login.aspx?email=' + strings[0] + '&nome=' + strings[1];
},
error: function () { }
})
}
Upvotes: 2