【问题标题】:Webservices with C#, Ajax, Jquery NOT working with ASP.Net使用 C#、Ajax、Jquery 的 Web 服务不使用 ASP.Net
【发布时间】:2013-02-11 04:37:09
【问题描述】:

我在 C# 中创建了一个WebService。在其中我有一个方法:
我在每个页面上使用相同的方法创建了一个AndroidWebservices.asmx 和一个Default.aspx

[System.Web.Services.WebMethod]
public bool CheckLogin(string email, string password)
{
    //Get the User Information
    DB.User cur_user = DB.User.ByEmail(email.Trim());

    if (cur_user.CheckPassword(password)) {
       return true;
    } else {
       return false;
    }
}

对于我输入的 Default.asxp 页面
<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" EnablePartialRendering="true" runat="server"></asp:ScriptManager>

对于 AndroidWebService.asmx,我取消了注释
[System.Web.Script.Services.ScriptService]


此服务正在运行 http://localhost:49524/ 以及我公司的 .com 域

我在 tomcat 服务器上新建了一个目录:

http://  localhost:8080/

这个代码是一个简单的表格

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="user-scalable=no, width=device-width" />
    <link rel="stylesheet" type="text/css" href="css/iphone.css">
    <link rel="stylesheet" 
        href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
    <script src="js/iphone.js"></script>
    <title></title>
</head>
<body>
    <div id="container">
        <div id="header">
            <h1><a></a></h1>
            <div class="loginContainer">
                <table cellpadding="0" cellspacing="0" width="100%">
                    <tr>
                        <td valign="top" style="width:100px">
                            <div class="rounded-end">email</div>
                        </td>
                        <td valign="top">
                            <input type="text" id="email" class="login" />
                        </td>
                    </tr>
                    <tr>
                        <td valign="top" style="width:100px">
                            <div class="rounded-end">password</div>
                        </td>
                        <td valign="top">
                            <input type="password" id="password" 
                                class="login" />
                        </td>
                    </tr>
                </table>
                <input type="button" class="loginButton" value="login" 
                    onclick="loginCheck(); return false;"/>
            </div>      
        </div>
    </div>
</body>
</html>

还有 Javascript:

function loginCheck() {

    var u = $('#email').val();
    var p = $('#password').val();

    $.ajax({
        type: "POST",
        url: "http://localhost:49524/mobile/Android/Default.aspx/CheckLogin",
        data: JSON.stringify({email: u, password: p}),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            alert(msg);
        }
    });
}

我尝试过的事情:

dataType: "json"

什么都不返回。

dataType: "jsonp"

返回页面..即

"<!doctype><html> ...... "

每当我将Default.aspx 作为我的 url 运行时,该函数都会返回页面的 html/文本,我可以将代码和 + 查询字符串放在 URL 中,然后我会返回页面.. 但没有数据 它似乎没有调用该方法,而只是返回页面..如上所述.. 我所追求的是返回 true 或 false 以确保用户登录以继续到下一页。

我非常感谢人们发表评论,我必须回到办公室才能重新开始工作。一旦我得到一个有效的答案,我会标记一个接受的答案。

【问题讨论】:

  • 我也在控制台上运行,所以我知道什么时候会出现同源错误
  • 您的if 块中似乎没有返回任何内容。
  • 这是一个错字,我返回的是真还是假
  • 请更新代码然后:)
  • 请告诉我您没有在客户端检查登录状态?!

标签: c# javascript jquery web-services


【解决方案1】:

我认为这是因为您将方法标记为受保护。将其标记为公开,然后重试。

所以

protected bool CheckLogin(string email, string password)

变成

public bool CheckLogin(string email, string password)

【讨论】:

  • 这并没有解决错误,但我确实做出了改变,因为你是对的,它需要公开而不是保护..但我仍然得到 html 作为响应而不是布尔值。
【解决方案2】:

我使用了一个 $.jsonp plugin 来节省时间它使 ajax 调用更容易..
我还必须把它放在我的 webconfig 中:

<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-23
    • 2013-07-04
    • 2021-05-27
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    相关资源
    最近更新 更多