简体   繁体   中英

jQuery AJAX call getting HTML response

I have an AJAX call in a .aspx file:

var data = { "schoolID": id };

$.ajax({
    type: "POST"
    , url: "schools.aspx/DeleteSchool"
    , data: JSON.stringify(data)
    , contentType: "application/json; charset=utf-8"
    , dataType: "json"
    , error: function (XMLHttpRequest, textStatus, errorThrown) {
        $.alert({
            title: "Error deleting school"
            , type: "red"
            , content: XMLHttpRequest + "||\n" + textStatus + "||\n" + errorThrown
        });
});

It's called when a button is clicked. It's supposed to pass an id to this C# function:

[WebMethod]
public void DeleteSchool(int schoolID)
{
    string query = @"
        DELETE
        FROM tableName
        WHERE id = @schoolID
    ";

    List<SQLParameter> parameters = new List<SQLParameter> {
        new SQLParameter("@schoolID", schoolID)
    };

    SqlConn.doQuery(query, parameters);
}

However, when I put a breakpoint in the C# function, it never breaks and the error function in the AJAX object always runs. The error looks like this:

Firefox Developer Edition中的错误图像

In Chrome, it looks like this:

Chrome中的错误图片

The response come back as HTML:

反应的形象

反应的形象

When I check the parameters of the request, it seems the JSON object that I pass is formatting correctly, so I'm not exactly sure what's wrong here, or why the WebMethod is never hit. I checked the MDN documentation for the 302 code , but I'm not sure if that's related to my problem at all. Based on my research, it appears that AJAX is expecting JSON instead of HTML and can't parse the response, but I'm confused as to why, since the return of the WebMethod is void and it isn't being hit anyway. Why am I getting an HTML response and why isn't my WebMethod being called?

your url in the ajax call need to be

url: " http://localhost:56302/context/Math/schools.aspx/DeleteSchool "

事实证明,问题出在我组织处理请求的方式上。

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