Calling ASP.NET web service function via GET metho

2019-04-28 11:36发布

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?

7条回答
仙女界的扛把子
2楼-- · 2019-04-28 11:53

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.

查看更多
冷血范
3楼-- · 2019-04-28 11:55

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

查看更多
女痞
4楼-- · 2019-04-28 12:00

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

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string Test2()
{
   [...]
}
查看更多
爷、活的狠高调
5楼-- · 2019-04-28 12:08
  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.

查看更多
女痞
6楼-- · 2019-04-28 12:09

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

查看更多
一夜七次
7楼-- · 2019-04-28 12:10
  1. You can use http handler instead of web service.
  2. You can parse xml response with javascript on the client.
查看更多
登录 后发表回答