Calling ASMX from jQuery

I am trying to call an ASMX method from jQuery without success. Following is my code, and I don't understand what I am missing.

File Something.js,

function setQuestion() {
    $.ajax({
        type: "POST",
        data: "{}",
        dataType: "json",
        url: "http: //localhost/BoATransformation/Survey.asmx/GetSurvey",
        contentType: "application/json; charset=utf-8",
        success: onSuccess
    });
}

function onSuccess(msg) {
    $("#questionCxt").append(msg);
}

File SomethingElse.cs,

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Survey : System.Web.Services.WebService {

    public Survey () {
    }

    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public string GetSurvey() {
        return "Question: Who is Snoopy?";
    }
}

One thing that stands out is you have UseHttpGet=true but in your jQuery code you are using POST.

Also here is a test page I created calling an ASMX page.

[WebMethod]
public Catalog[] GetCatalog()
{
    Catalog[] catalog = new Catalog[1];
    Catalog cat = new Catalog();
    cat.Author = "Jim";
    cat.BookName ="His Book";
    catalog.SetValue(cat, 0);
    return catalog;
}

<script type="text/javascript">
    $(document).ready(function() {
    $.ajax({
            type: "POST",
            url: "default.asmx/GetCatalog",
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: "{}",
            dataType: "json",
            success: handleHtml,
            error: ajaxFailed
        });
    });

    function handleHtml(data, status) {
        for (var count in data.d) {
            alert(data.d[count].Author);
            alert(data.d[count].BookName);
        }
    }

    function ajaxFailed(xmlRequest) {
        alert(xmlRequest.status + ' \n\r ' + 
              xmlRequest.statusText + '\n\r' + 
              xmlRequest.responseText);
    }
</script>

You have to make sure you specify Json as the response format if that is what you want and get rid of UseHttpGet due to security features:

[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string GetSurvey() {
    return "Question: Who is Snoopy?";
}

Here is an example of a jQuery call to a page method on an aspx, but it would be similar to an asmx page.

$.ajax(
    {
        type: "POST",
        url: "NDQA.aspx/ValidateRoleName",
        data: '{"roleName":"' + $('[id$=RoleNameTextBox]').val() + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: ValidateSuccess,
        error: ValidateError

    });

I came across this question and had the same issue. I solved it by adding:

[WebInvoke(Method="POST",ResponseFormat=WebMessageFormat.Json)]

Below your web method attribute, if you'd like to use POST. ie:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Survey : System.Web.Services.WebService {

    public Survey () {
    }

    [WebMethod]
    [WebInvoke(Method="POST",ResponseFormat=WebMessageFormat.Json)]
    [ScriptMethod(UseHttpGet = true)]
    public string GetSurvey() {
        return "Question: Who is Snoopy?";
    }
}

I would also suggest removing UseHttpGet as Jim Scott suggested.

You can add the following to your options and check the objXMLHttpRequest to see a more detailed error response.

error: function(objXMLHttpRequest, textStatus, errorThrown) {
 debugger;               
}