Calling webservice asmx from ajax returns 404 error

Viewed 91

I have an .asmx file that I'm calling from an jquery ajax call on a webforms page. The response returned is a 404 error. I'm not sure what I'm doing wrong as I've tried all examples that I have found and nothing is working.

AJAX:

function clearTemp(_websiteID) {
        $.ajax({
            type: "POST",
            url: "ScheduleImportWebService.asmx/HelloWorld",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({ websiteID: _websiteID }),                
            dataType: "json",
            success: function (response) {
                alert(response.d);                   
            },
            failure: function (response) {
                alert("Failure: Could not clear temp table: " + response.d);
            },
            error: function (response) {
                alert("Error: Could not clear temp table: " + response.d);
            }
        });
    }

ASMX Snippet: (I've tried with the [System.Web.Script.Services.ScriptService] commented out as well and still get the same response)

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class ScheduleImportWebService : System.Web.Services.WebService
{

    [WebMethod]
    public string HelloWorld(string websiteID)
    {
        return "Hello World";
    }
}
1 Answers

The error is because you are forgetting to set the host in URL from ajax call.

Try something like this:

http://localhost/ScheduleImportWebService.asmx/HelloWorld
Related