0

I am trying to use ajax call to get the data from web services whose function has a return value of string in json format.

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class MyClass : WebService {
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]

    public Chart RetrieveData(string countries, string startDate, string endDate)
    {
        Chart c = getData(countries, startDate, endDate);
        return c;
    }

}

And I am using Ajax like following:

var path = 'pathToXml/file.asmx/RetrieveData?countries=' + countries + '&startDate=' + startDate + '&endDate=' + endDate;
$.ajax({
    type: "GET",
    url: path,
    contentType: 'application/json; charset=utf-8',
    success: function (data) {
        console.log(data.d);
    },
    error: function (textStatus, errorThrown) {
        console.log(textStatus);
        console.log(errorThrown);
    }
});

I have read SO posts like this, and this. However, none of these worked

AngryOtter
  • 149
  • 9
  • you need to access the `data.d` or `msg.d` values... https://stackoverflow.com/a/36822653/4000335 – kshkarin Mar 31 '21 at 17:11
  • @kshkarin for my first ajax call, data.d gives me 'underfine' regardless of I specific the datatype as "text" or not. and my second ajax simply had error, so I don't think this will work. – AngryOtter Mar 31 '21 at 17:15

1 Answers1

0

UseHttpGet = true is required in the ScriptMethod attribute, otherwise this error results:

An attempt was made to call the method 'RetrieveData' using a GET request, which is not allowed.

MyWebService.asmx

<%@ WebService Language="C#" Class="MyWebService" %>

using System.Web.Script.Services;
using System.Web.Services;

[ScriptService]
public class MyWebService : WebService
{
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
    public object RetrieveData(string countries, string startDate, string endDate)
    {
        return new
        {
            countries,
            startDate,
            endDate
        };
    }
}

index.html

<!DOCTYPE html>
<html>
<body>
<button onclick="retrieveData('usa','6/18/2012','4/1/2021')" type="button">Retrieve Data</button>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
        integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
        crossorigin="anonymous"></script>
<script>
    function retrieveData(countries, startDate, endDate) {
        const path = `/MyWebService.asmx/RetrieveData?countries='${countries}'&startDate='${startDate}'&endDate='${endDate}'`;
        $.ajax({
            type: 'GET',
            url: path,
            contentType: 'application/json; charset=utf-8',
            success: function(data) {
                console.log(data.d);
            }
        });
    }
</script>
</body>
</html>
Chris Carter
  • 103
  • 7
  • I am not posting data to the server, rather getting the data back from the server. – AngryOtter Apr 01 '21 at 17:53
  • @AngryOtter example is updated to be a GET. Added querystring param for id and `UseHttpGet = true` added to the ScriptMethod attribute. – Chris Carter Apr 01 '21 at 18:59
  • It doesn't work. The solution you provided was similar to the solution from the posts I linked in my question. By using your example, I get:`An attempt was made to call the method 'RetrieveChart' using a GET request, which is not allowed`. If I change to post, it will say I am missing parameter value. – AngryOtter Apr 05 '21 at 19:49
  • @AngryOtter cleaned up the working example so it looks like what you started with, removed my extra example. – Chris Carter Apr 06 '21 at 21:46
  • I have cleaned my example. I know how to get the json string within the XML node, but I want the Web Service to return actual JSON object instead of XMLdocument with Json string as node value. – AngryOtter Apr 08 '21 at 18:25
  • @AngryOtter Add `UseHttpGet=true` to your ScriptMethod attribute. remove `dataType:text` from the ajax request, add `content-type: 'application/json; charset=utf-8'` to your ajax request. Also, return the `SomeClass` instance from your WebMethod, don't convert it to a string. My example only uses json, no xml. It's a working example, you can cut and paste into a local project to see it work. – Chris Carter Apr 08 '21 at 22:01
  • My apologies for late reply as I was caught into some other project, but no. Even this doesn't work. I have updated my answer and by using that, I am getting "invalid Json Primitive" – AngryOtter Apr 28 '21 at 14:30
  • @AngryOtter you get that because you're not quoting the values in the querystring: `var path = `pathToXml/file.asmx/RetrieveData?countries='${countries}'&startDate='${startDate}'&endDate='${endDate}'`;` the back ticks are messing with the format. Look at my working example for how I'm using the back ticks. Here is the code solution for reference,[https://github.com/chrcar01/AsmxReturningJsonDemo](https://github.com/chrcar01/AsmxReturningJsonDemo) – Chris Carter Apr 29 '21 at 16:38