[ACCEPTED]-Calling ASMX from jQuery-asmx

Accepted answer
Score: 28

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

Also 2 here is a test page I created calling an 1 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>
Score: 6

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

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

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

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

Below 2 your web method attribute, if you'd like 1 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?";
    }
}
Score: 2

Here is an example of a jQuery call to a 2 page method on an aspx, but it would be 1 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

    });
Score: 1

I would also suggest removing UseHttpGet 3 as Jim Scott suggested.

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

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

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

If you read that article then you 9 would see that it is safe to use UseHttpGet 8 as ASP.NET has features to block the cross 7 site scripting attack vector.

There are plenty 6 of valid reasons to use GET.

He can remove 5 the data parameter and change POST to GET 4 to make the call work. Assuming you want 3 a JSON response it would be required to 2 add ResponseFormat=ResponseFormat.Json as 1 well.

Score: 1

The following Steps solved my problem, hope 8 it helps some one,

  1. To allow this Web Service 7 to be called from script, using ASP.NET 6 AJAX, include the following line above your 5 asmx service class for example

    [System.Web.Script.Services.ScriptService] public 4 class GetData : System.Web.Services.WebService {

  2. Add 3 protocols under system.web in web.config, please 2 click on the link if you are not able to 1 view configuration

https://pastebin.com/CbhjsXZj

<system.web>
<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>

Score: 0

If you try chrome browser, try internet 4 explorer it worked for me and also it is 3 about chrome browser you must add extension 2 to works in chrome but i dont know the name 1 of extension

More Related questions