views:

310

answers:

7

Hi, 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?

A: 
  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.

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

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).

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

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.

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

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).

Zhaph - Ben Duguid
[ScriptMethod] attribute doesn't take any effect
the_V
A: 

http://stackoverflow.com/questions/618900/enable-asp-net-asmx-web-service-for-http-post-get-requests

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string Test2()
{
   [...]
}
Mike S
A: 

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")]

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