1

I use the jQuery.ajax function to make a GET request to the following asp.net webservice:

[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string GetAgentsGroupNameById(int id)

I set the contentType to be 'application\json' and the dataType to be 'jsonp' but I still get an xml response:

<?xml version="1.0" encoding="utf-8"?> 
<string xmlns="tempuri.org/">Sports</string>;

Here are the details of the request and the response, as shown in the chrome's dev tools: enter image description here

Am I doing something wrong with the request? or maybe with the webservice itself?

benams
  • 4,308
  • 9
  • 32
  • 74
  • you need to return a string rapresentation of a JSON object for it to work. What is the server actual return? – Carlo Moretti Jul 12 '12 at 08:43
  • I think that's what the webservice returns... from the code: return Newtonsoft.Json.JsonConvert.SerializeObject(agent); – benams Jul 12 '12 at 08:54

2 Answers2

0

Webservice can return both JSON and XML, you will have to specify which format you want in your ajax call like this contentType: 'application/json; charset=utf-8'. The serialization is automatic. Don't worry your code will work fine. The content returned is of the form

  <?xml version="1.0" encoding="utf-8" ?> 
        <string xmlns="http://formshare.com/">
           [{"VAL1":"SSDSDSD"},{"VAL2":"PDWDWWD"}]//Example JSON
        </string>
Ashwin Singh
  • 7,197
  • 4
  • 36
  • 55
  • please read my question. I mentioned that I used the 'application/json' as a contentType (btw, I have also tried 'application/json; charset=utf-8'. It didn't help. – benams Jul 12 '12 at 09:05
  • Have a re-look at the answer. – Ashwin Singh Jul 12 '12 at 09:08
  • Thanks for your help. But... can you be more specific please? I have to serialize the XML response somehow? If not, what should I do in order to get a json response? (I set the contentType to 'application/json; charset=utf-8', In addition my web method has attribute of: [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]. ) – benams Jul 12 '12 at 09:16
  • First have a look at this question http://stackoverflow.com/questions/1121559/asp-net-json-web-service-always-return-the-json-response-wrapped-in-xml and then tell me what you want next. – Ashwin Singh Jul 12 '12 at 09:25
  • I read the question and the answers (no correct answer is marked). The only new suggestion I see are: making my web method static (really necessary?), returning a JSON object instead of a json string, set the dataType to be 'json' instead of 'jsonp' - then I won't have an optoin to make cross-domain requests. – benams Jul 12 '12 at 09:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13778/discussion-between-ashwin-singh-and-benams) – Ashwin Singh Jul 12 '12 at 10:13
0

Seems like I have to add a script tag that will wrap the response (in the web service). I know that it can be done automatically with WCF, but I'm not sure if asp.net webservices can do the job for me, so I'll add an HttpModule that will do it and let you know.

benams
  • 4,308
  • 9
  • 32
  • 74