views:

126

answers:

1

Hi all,
Making my first steps in trying to use all these technologies together.. I'm having some toubles..
Here is my Server side:

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string simplestMethod()
{
  return "Simplest method returned";
}

And here is my client side:

 $(document).ready(function(){
   $("a").click(function(event){     
      $.ajax({
      type: "POST",
      url: "http://localhost:53346/d2/TAPI.asmx/simplestMethod",
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function (data) {
       alert(data.d);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
       alert("Error Occured!" +" | " + XMLHttpRequest +" | " + textStatus +" | " + 
       errorThrown );
      }
   });
  });
 });

The result is an alert that says:
Error Occured! | [object XMLHttpRequest] | parseerror | undefined.
What parsing failed and why?
I should mention that calling the WS method directly does work.
Thanks a lot!

+1  A: 

Your code looks like good with one suspected place: url. You should replace url to something like "TAPI.asmx/simplestMethod" or "/d2/TAPI.asmx/simplestMethod".

Moreover if you want study to how to call web method with parameters or return more complex data from the web method look at http://stackoverflow.com/questions/2737525/how-do-i-build-a-json-object-to-send-to-an-ajax-webservice/2738086#2738086 and http://stackoverflow.com/questions/3445859/asmx-web-service-json-javascript-jquery/3446517#3446517, http://stackoverflow.com/questions/2670147/can-i-return-json-from-an-asmx-web-service-if-the-contenttype-is-not-json/2671583#2671583. How to decode error messages from the exception inside of web method see http://stackoverflow.com/questions/3635133/get-xhr-object-in-vb-net-while-ajax-calling-fails/3644248#3644248.

Oleg
No luck. changing the URL gives the same error, and when I change the error and success functions, I just get an alert that says "error". But thanks!
Oren A
@Oren A: You have textStatus=**parseerror**. So you should first of all show `xhr.responseText` with `alert(xhr.responseText);`
Oleg
@Oren A: By the way I tested your code. If work without any problem. So your **real** error is somewhere outside the code which you posted in your question.
Oleg
I've been reading.. Could it be that I should add something to the web.config? (I'm using .NET 4.0) What other places in the code are relevant? Thanks.
Oren A
alert(xhr.responseText); gives an empty alert...
Oren A
@Oren A: To make you easy to compare your solution with one working I created a simple application in .NET 4.0 (Visual Studio 2010) which do the same what you describe in your question. You can download full code from http://www.ok-soft-gmbh.com/ForStackOverflow/SimpleWebService.zip
Oleg
Thanks a lot Oleg. This task got the best of me, but I'll get back to it... Until than, I really can't ask for a better answer. Cheers!
Oren A