0

Im having a problem with my web service. When I pass the parameters as POST and in a JSON format, It returns a JSON object. I want to convert the POST to a GET but the problem is, the web service only returns in XML format.

<script>   
$.ajax({
        type: "GET",
        url: "/web_services/webservice.asmx/getReturnJson",
        data: "params1=1",
        contentType: "application/*; charset=utf-8",
        dataType: "json")
</script>

The web service:

    [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat=ResponseFormat.Json)]
    public List<tempStruct> getReturnJson(String params1)
    { 
     return temp;
    }
Cheran Shunmugavel
  • 8,319
  • 1
  • 33
  • 40
Milo Cabs
  • 503
  • 1
  • 10
  • 24

2 Answers2

0

if you are having problems with GET parameters not being properly parse set them in the URL.

$.ajax({
    type: "GET",
    url: "/web_services/webservice.asmx/getReturnJson?params=1",
    contentType: "application/json; charset=utf-8")

Also make sure you have all the configs appropriate.

<httpHandlers>
    <remove verb="*" path="*.asmx"/>
    <add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory"     validate="false"/>
</httpHandlers>
Samy Vilar
  • 10,800
  • 2
  • 39
  • 34
  • Currently it returns in XML format. I invoked the json format so that it would return a JSON object. what i noticed is that if i place contentType:"application/json" it would return a json object but it has problems with get parameters. when contentType is default, it takes in the get parameters but returns in an xml format – Milo Cabs Jun 13 '12 at 05:20
  • this might be able to help you http://stackoverflow.com/questions/2651091/jquery-ajax-call-to-httpget-webmethod-c-not-working – Samy Vilar Jun 13 '12 at 05:47
  • i also tried to include the parameters in the url.. it returns an error stating there is no web service for said url. it includes the parameters as the web service url name. – Milo Cabs Jun 13 '12 at 12:22
  • that would actually work but the main reason why i wanted it to be a GET method because i want my parameters to be in the usual url.com?param1=1&param2=2 – Milo Cabs Jun 13 '12 at 12:28
  • if `http://localhost/web_services/webservice.asmx/getReturnJson?params=1` didn't work than you may need to look at your web config, also please make sure `[System.Web.Script.Services.ScriptService]` is at the the top of your class definition. – Samy Vilar Jun 13 '12 at 19:55
0

Found out that it doesn't accept POST request for security reasons

Milo Cabs
  • 503
  • 1
  • 10
  • 24