7

I'm trying to call web service function via GET method using jQuery, but having a problem. This is a web service code:

[WebService(Namespace = "http://something.com/samples")]
[ScriptService]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class TestWebService  : System.Web.Services.WebService {
    [WebMethod]
    public string Test2()
    {
        string result = null;

    try
        {
            result = "{'result':'success', 'datetime':'" + DateTime.Now.ToString() + "'";
        }
        catch (Exception ex)
        {
            result = "Something wrong happened";
        }

        return result;
    }

}

That's the way I call the function:

$.ajax({ type: "GET",
         url: "http://localhost/testwebsite/TestWebService.asmx/Test2",
         data: "{}",
         contentType: "application/json",
         dataType: "json",
         error: function (xhr, status, error) {
             alert(xhr.responseText);
         },
         success: function (msg) {
             alert('Call was successful!');
         }
     });

Method is called successfully, but result string gets covered by XML tags, like this:

<string>
{'result':'success', 'datetime':'4/26/2010 12:11:18 PM'
</string>

And I get an error because of this (error handler is called). Does anybody know what can be done about this?

5
  • Where's your '}' on the end of the returned JSON string? Commented Apr 27, 2010 at 13:08
  • Do you mean json string covered by XML tags? I think I did something wrong when I copied it from the browser window, it should be there, of course. Commented Apr 27, 2010 at 16:00
  • Perhaps, but I don't see it in the web service code either. Commented Apr 27, 2010 at 17:56
  • What version of .Net are you using? Commented Apr 29, 2010 at 14:34
  • See this question: stackoverflow.com/questions/288850/… Commented Apr 29, 2010 at 18:28

7 Answers 7

9

Enable ASP.NET ASMX web service for HTTP POST / GET requests

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string Test2()
{
   [...]
}
Sign up to request clarification or add additional context in comments.

Comments

0
  1. Rule for json: You can only access data from the same domain!

  2. The only exception is when using jsonp (which is quite complicated to implement since there is no jsonp serializer in the .NET framework).
    If your are using a standard web service (and not WCF) you can find guidance howto implement this here.

1 Comment

For now it should be enough for me to access data within the same domain. But even if I open URL like this localhost/testwebsite/TestWebService.asmx/Test2 in browser on my local machine, I see XML rendered. The only thing I need now is to get rid of XML
0

Make sure to add this to your ajax options:

contentType: "application/json; charset=utf-8"

Your overall request should look like this to get json back instead of XML:

$.ajax({ type: "GET",
     url: "http://localhost/testwebsite/TestWebService.asmx/Test2",
     data: "{}",
     contentType: "application/json",
     dataType: "json",
     contentType: "application/json; charset=utf-8".
     error: function (xhr, status, error) {
         alert(xhr.responseText);
     },
     success: function (msg) {
         alert('Call was successful!');
     }
 });

ScottGu has a full breakdown on what's required here, but it looks like the missing contentType in your case (that one drove me nuts for a while too).

5 Comments

Changing contentType didn't take any effect
@the_V - What error is it alerting? also just for kicks, do you get the same behavior with POST?
POST works fine, but I want to get it working with GET. About error: xhr.responseText is following - <string xmlns="something.com/samples">{'result':'success', 'datetime':'27.04.2010 0:57:30'</string>
@the_V - Try changing [ScriptMethod] to [ScriptMethod(UseHttpGet=true)] on Test2, see what your response is.
Response is the same, string gets covered with XML stuff
0

You might try setting the ResponseFormat on your methods. See http://williamsportwebdeveloper.com/cgi/wp/?p=494 to see how they did it for JSON. It probably just defaults to XML.

1 Comment

I've tried to add following attribute to method definition: [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true, XmlSerializeString=false)] but it didn't work
0

You need to decorate the method with the ScriptMethodAttribute:

[WebService(Namespace = "http://something.com/samples")]
[ScriptService]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class TestWebService  : System.Web.Services.WebService {

  [WebMethod]
  [ScriptMethod]
  public string Test2()
  {
    [...]
  }
}

This will ensure that the method returns JSON by default (the default value of ResponseFormat is Json).

1 Comment

[ScriptMethod] attribute doesn't take any effect
0

Did you try WebInvokeAttribute, it has members that define Request & Response formats where you can set to WebMessageFormat.Json.
Something like:
[WebInvoke(UriTemplate = "ServiceName", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, Method = "POST")]

Comments

0
  1. You can use http handler instead of web service.
  2. You can parse xml response with javascript on the client.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.