在Ajax函数后面的代码上调用c#函数 [英] Call c# function on code behind from Ajax function

查看:144
本文介绍了在Ajax函数后面的代码上调用c#函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试获取函数背后的代码,以加密两个变量以作为查询字符串返回.但是我一直没有成功.第一次尝试Ajax.

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);
}

在我的ASP中,我有这个(实现Facebook登录):

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

在尝试Ajax之前,它会工作,而无需尝试获取后面的代码.我是这样的:

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.

我是这样的:

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); }
            })

在后面的代码中:

[WebMethod]
public static string EncritaDados(string email, string nome)
{
    return Verificacoes.EncryptString(email) + ";" + Verificacoes.EncryptString(nome);
}

因此,基本上,我做了一些小的更改,但由于数据字符串格式错误,因此未按预期执行操作.但是我按照JSON.stringify的建议对它进行了格式化,并且可以正常工作.

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.

推荐答案

您的JavaScript错误. resp.d而不是resp.data:

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

这篇关于在Ajax函数后面的代码上调用c#函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆