简体   繁体   中英

Call c# function on code behind from Ajax function

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.

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 () { }
        })
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM