tags:

views:

3883

answers:

5

Hi,

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

Thanks, Ebe

///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);
}


///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?";
    }

}
+5  A: 

One things that stands out is you have UseHttpGet=true but in your jquery you are using POST.

Also here is a test page I created calling a 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>
Jim Scott
A: 

here is an example of a jquery call to a page method on an aspx, but 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

    });
CSharpAtl
+1  A: 

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;               
}
Rick Hochstetler
+1  A: 

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?";
}
Josef
A: 

thanks, guys, now it is working... Just using POST at the both sides

Can you tell me what do you mean "both sides"?
alienavatar