preeti sharma
preeti sharma

Reputation: 11

Call WebService method by jQuery

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?

Upvotes: 0

Views: 723

Answers (3)

M.Azad
M.Azad

Reputation: 3763

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

Upvotes: 1

AmbrosiaDevelopments
AmbrosiaDevelopments

Reputation: 2592

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.

Upvotes: -1

Ram Singh
Ram Singh

Reputation: 6938

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

Upvotes: 1

Related Questions