0

I'm trying to call a WebService method by jQuery, but it's not working.

The code is given below...

jQuery

$.ajax({
    type: "POST",
    data: "{}",
    dataType: "json",
    url:'test.asmx/GetSurvey',
    contentType:"application/json;charset=utf-8",
    success: function(data) {
        $("#Span1").html(data.d);
    }
});

test.asmx (WebService) code given below:

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

What could be the problem?

Liam
  • 27,717
  • 28
  • 128
  • 190

3 Answers3

1

here jquery code :

$.ajax({
type : "POST",
data : "",
dataType : "json",
url : 'test.asmx/GetSurvey',
contentType : "application/json;charset=utf-8",
success : function(data) {
    $("#Span1").html(data);
 }
});

You have copy pasted the code from some where and didnt remove 'enter your code' in url so that was the problem..

EDITED:

url: '<%=ResolveUrl("~/test.asmx/GetSurvey") %>',

try to pass the path in such way

Ram Singh
  • 6,664
  • 35
  • 100
  • 166
1

in your ajax call

$.ajax({
type: "POST",
data: "{}",
dataType: "json",
url:'test.asmx/GetSurvey',
contentType:"application/json;charset=utf-8",
success: function(data) {
console.log(data);
    $("#Span1").html(data.d);
}
});

you can see this log with Chrome devtools in Console Tab. In addition you can monitor your request/response with Chrome devtools in Network Tab

Check it for more information about Chrome devtools Chrome Dev Tools: Networking and the Console

M.Azad
  • 3,673
  • 8
  • 47
  • 77
-1

1) "Question: Who is Snoopy?" is not JSON.

2) data.d references nothing.

3) enter code here is jibberish, remove it.

4) If you don't have data to send then you can leave out data in your AJAX options, "{}" is the wrong way to define your parameters anyway.

Have a read of this to get a good idea of how to send/return JSON and use it correctly.

Community
  • 1
  • 1
AmbrosiaDevelopments
  • 2,576
  • 21
  • 28
  • 1) my code is returning string..so json is correct...i used it earlier too...2)data.d will return the 'Question: Who is Snoopy?' 3)'enter code here' is for user to understand its not wriiten by me...when i post this question its automatically write that – preeti sharma Apr 26 '13 at 06:45