简体   繁体   English

AJAX无法成功从ASP.NET服务器返回

[英]AJAX not success returning from asp.net server

as practicing Ajax, I wrote a Ajax call that sends to a local asp.net Handler.ashx a Json with a username and password, and returns true if they are equal to "jhon" "123456", else false. 在练习Ajax时,我编写了一个Ajax调用,该调用将使用用户名和密码的Json发送到本地asp.net Handler.ashx,如果它们等于“ jhon”“ 123456”,则返回true,否则返回false。 I debugged the code, and I saw that the Handler.ashx receives the call, does the validation, and write to response, but the Ajax call not success. 我调试了代码,然后看到Handler.ashx收到了调用,进行了验证,并写入了响应,但是Ajax调用未成功。

this the Ajax call: 这是Ajax的调用:

$.ajax({
    url: '/Handler.ashx',
    dataType: 'json',
    data: {
        name: document.getElementById("username").value,
        password: document.getElementById("password").value
    },

    success: function (json) {
        alert(json.isvalid);
    },
    error: function (jqXHR, textStatus, errorThrown) {
        console.log(textStatus, errorThrown);
        alert(textStatus + "  " + errorThrown);
    }
});
alert("failed");

and this is the server side: 这是服务器端:

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        Console.WriteLine("check");
        var name = context.Request["name"];
        var password = context.Request["password"];

        string response = IsValid(name, password) ? "true" : "false";
        context.Response.ContentType = "appliaction/text";
        context.Response.Write("{isvalid:'" + response + "'}");
    }

    private bool IsValid(string name, string password)
    {
        Console.WriteLine("isvalid");
        return (name == "jhon" && password == "123456");
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

thanks! 谢谢!

The simplest change (which still isn't very pretty) would be to change this... 最简单的更改(仍然不是很漂亮)是更改此...

string response = IsValid(name, password) ? "true" : "false";
context.Response.ContentType = "appliaction/text";
context.Response.Write("{isvalid:'" + response + "'}");

...to this... ...对此...

string response = IsValid(name, password) ? "true" : "false";
context.Response.ContentType = "application/json";
context.Response.Write("{ \"isvalid\" : \"" + response + "\" }");

(Note that you'd misspelt "application"... and you should tell your caller than you're returning json data.) (请注意,您会拼写“应用程序” ...,并且应该告诉调用者,而不是返回json数据。)

I saw that the Handler.ashx receives the call, does the validation, and write to response, but the Ajax call not success. 我看到Handler.ashx收到了调用,进行了验证,并写入了响应,但是Ajax调用没有成功。

When you say the "AJAX call not success", does an error appear ? 当您说“ AJAX呼叫不成功”时,是否出现错误?

I would recommend running your code in Google Chrome, open the Developer Options (by hitting F12), go into the Network tab, then refresh your webpage. 我建议您在Google Chrome中运行您的代码,打开“开发人员选项”(按F12键),进入“网络”标签,然后刷新您的网页。

Watch the Network tab, click on the URL being called, then check the Request being sent, and the Response coming back. 观看“网络”选项卡,单击要调用的URL,然后检查“正在发送的请求”和“响应”。

Then also check the Console tab, to see if any JavaScript errors have quietly been thrown. 然后还要检查“控制台”选项卡,以查看是否有任何JavaScript错误被悄悄抛出。

try adding this to your ProcessRequest method 尝试将其添加到您的ProcessRequest方法中

JavaScriptSerializer serializer = new JavaScriptSerializer();

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void ProcessRequest(HttpContext context)
    {
        Console.WriteLine("check");
        ....

        return serializer.Serialize("{ isvalid: '" + response + "' }");
    }

And in the client side 而在客户端

.....
success: function (msg) {
    console.log(msg); //this is for you to see the response in the JS console
    var jsonArray = $.parseJSON(msg.d);
    console.log(jsonArray); //and this is the same
    alert(jsonArray.isvalid);
}
.....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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