-1

I've asp.net webservice with json returning data, when I call it, it return me data in json but embed it in xml.

What should I do on the server side to ensure that my webservice just returns json?

My .asmx service is as below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using System.Text;
using System.Collections;
using System.IO;
using System.Xml;

[WebMethod(Description = "DemoMethod to get Total.")]
public string GetTotal(string a, string b, string c)
{
    List<Hashtable> objMyclass = new List<Hashtable>();
    JSonOutPutProperties jsonProperty = new JSonOutPutProperties();
    // 
    int total = Convert.ToInt32(a) + Convert.ToInt32(b) + Convert.ToInt32(c);
    jsonProperty.Properties.Add("Total", total);
    objMyclass.Add(jsonProperty.Properties);
    //
    JsonOutput objjson = new JsonOutput();
    objjson.objectcount = objMyclass.Count;
    objjson.objectname = "Total";
    objjson.objectvalues = objMyclass;
    //
    JavaScriptSerializer js = new JavaScriptSerializer();
    string strJSON = js.Serialize(objjson);
    return strJSON;
}
surfmuggle
  • 5,527
  • 7
  • 48
  • 77
Manish Jain
  • 842
  • 1
  • 11
  • 29
  • Could you clarify your question. For example what is the end of the url; what kind of service (asmx, svc) are you using. Can you post a part of the method from your webservice that returns the json? – surfmuggle Oct 26 '12 at 14:29
  • Hello threefouronesixonethree, post updated with code, have any idea what is wrong – Manish Jain Oct 27 '12 at 10:22
  • High Deer could you solve the issue by adding the line `[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]`? What is the result after you add the attribute? – surfmuggle Oct 28 '12 at 08:14

1 Answers1

0

The problem may be solved if you add the following line to your method:

[WebMethod(Description = "DemoMethod to get Total.")]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public string GetTotal(string a, string b, string c)
{
    ...

And you probably have to make sure that you are using post instead of get.. Scott Guthrie has another good post on json

Please take a look at the following question how-to-let-an-asmx-file-output-json or the other links in that post. I had a similar problem once.

Community
  • 1
  • 1
surfmuggle
  • 5,527
  • 7
  • 48
  • 77
  • Can you provide more information. Have you debugged the objjson, jsonProperty and objMyclass. Who is calling whom: in the screenshot you have a browser open so i assume that your Service can be accessed by entering a url into the browser. What does it look like if you open it in another browser; what is the url to call the method and so on. Without more information this is fishing in the dark without a fishing rod. – surfmuggle Oct 29 '12 at 11:03