1

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.

1 Answer 1

2

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 () { }
        })
    }
Sign up to request clarification or add additional context in comments.

7 Comments

Where is that RequestObject object? The thing is, I want those two two, which are a name and an email to be passed to the c# method to be encrypted and then return them. So, I don't exactly have an object to pass.
Edited my response to include the RequestObject class. You can name your two properties any way you want, just be sure to name them the same in the C# class and the javascript object.
I can't seem to get it to work. I know it gets to the Ajax function. I put an allert there and it gets there. But it appears to just stop there. It doesn't appear to go into the $.ajax function... :(
It does enter the function, after all. But I get an internal server error... I believe it's error 500
I've been messing around and I couldn't seem to get it with an object. But with string as I had I did it. Basically, it was the 'resp.data.split(";");' that had to be 'resp.d.split(";");' and the 'JSON.stringify(requestObject)' as you suggested. I posted the code in the main post. Please, make the necessary changes so I can accept your answer. And thanks, btw. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.