0

我有一个在本地服务器上完美运行的 AJAX 调用。我正在尝试通过 C#(来自另一个应用程序/域)进行相同的调用,但我很困惑。

这是我的网络方法:

[WebMethod(true)]
public static object ParentLogin(string email, string password)
{
    List<object> ChildrenList = new List<object>();

    Usuario loggedUser = GetByEmailAndPass(email, password);
    if (loggedUser != null)
    {
        var children = Factory.Current.GetInstance<IChildrenBIZ>().GetAll().ToList();

        foreach (var item in children )
        {
            ChildrenList.Add(new { userid = item.Id, name = item.Nome });
        }
        return new { success = true, email = loggedUser.Login, users = ChildrenList};
    }

    return new { success = false };
}

这是我的ajax调用:

var url = "http://localhost/" + "LoginTest/Login.aspx/ParentLogin";
    var dataParams = "{ email: " + "'myemail@hotmail.com'" + ", password: " + "'123123'" + " }";

    ExecuteAjaxCall(url, dataParams, true, function (msg) {
        if (msg.d) {
            var success = msg.d.success; 
            var children = msg.d.users;

            if (children.length > 0) {
                $.each(children, function (i, item) {
                    if (item) {

                        var childID = item.userid;
                    }
                });
            }
        }

    }, StandardError);

});

现在我想通过后面的代码调用这个相同的 Webthod。我尝试了使用 Restsharper,但我无法实现。请问有人吗?

编辑:

这是 ExecuteAjaxCall 方法:

function ExecuteAjaxCall(url, dataParam, isAssync, callBackSucess, callBackError) {
$.ajax({
    type: "POST",
    url: url,
    data: dataParam,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    assync: isAssync,
    success: function (msg) {
        callBackSucess(msg);
    },
    error: function (msg) {
        callBackError(ERROR_MSG);
    }
});
}

重要的:

我想要这个的主要原因是因为我想从另一个域使用 ParentLogin 方法。可能来自 iOS 或 Android 应用程序!这是正确的方法吗?

谢谢!

4

2 回答 2

3

此 Web 方法(实际上是“页面方法”)是 Web 应用程序中的公共静态方法。你应该可以调用它:

namespace.Login.ParentLogin(email, password);
于 2013-05-21T23:47:22.400 回答
1

我会重构一个服务类来保存你的应用程序逻辑,这样你就可以在另一个项目而不是 RestSharp 中使用它。

[WebMethod(true)]
public static object ParentLogin(string email, string password)
{
    return ServiceAdapter.ParentLogin(email, password);
}

public interface IService
{
    object ParentLogin(string email, string password);
}

public sealed class Service : IService
{
    public object ParentLogin(string email, string password)
    {
        List<object> ChildrenList = new List<object>();

        Usuario loggedUser = GetByEmailAndPass(email, password);
        if (loggedUser != null)
        {
            var children = Factory.Current.GetInstance<IChildrenBIZ>().GetAll().ToList();

            foreach (var item in children )
            {
                ChildrenList.Add(new { userid = item.Id, name = item.Nome });
            }
            return new { success = true, email = loggedUser.Login, users = ChildrenList};
        }

        return new { success = false };
    }

    //TODO move GetByEmailAndPass
}

public sealed class ServiceAdapter
{
    public static readonly IService Service = new Service();
}
于 2013-05-22T00:02:09.037 回答