Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webservice returns json data but having xml header how can we remove

Tags:

.net

asp.net

Am using simple webservice ".asmx".In my web service returning some data like:

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

{"Table":[{"minlatency":16.0,"Time":"\/Date(1328248782660+0530)\/"},{"minlatency":7.0,"Time":"\/Date(1328248784677+0530)\/"},{"minlatency":13.0,"Time":"\/Date(1328248786690+0530)\/"},{"minlatency":6.0,"Time":"\/Date(1328248788690+0530)\/"},{"minlatency":20.0,"Time":"\/Date(1328248790707+0530)\/"}]}</string>

I am using:

[ScriptMethod(ResponseFormat = ResponseFormat.Json,  XmlSerializeString = false)]

after [webmethod]in my service,and it is giving error in ajax callback,i want return values in json format.

like image 364
user1208909 Avatar asked Dec 31 '25 00:12

user1208909


1 Answers

I had the same issue a few weeks ago, I found that using the following as my service

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void GetPointers() {
    DataTable dtmkrs = new DataTable();
    dtmkrs.Load(SPs.GetPointers().GetReader());

    string[][] pointerArray = new string[dtmkrs.Rows.Count][];
    int i = 0;
    foreach (DataRow mkrs in dtmkrs.Rows)
    {
        pointerArray[i] = new string[] { mkrs["LocationID"].ToString(), mkrs["Title"].ToString(), mkrs["Blurb"].ToString(), mkrs["Url"].ToString(), mkrs["LongLatPoint"].ToString() };
        i++;
    }

    JavaScriptSerializer js = new JavaScriptSerializer();
    string strJSON = js.Serialize(pointerArray);
    dtmkrs.Dispose();
    Context.Response.Clear();
    Context.Response.ContentType = "application/json";
    Context.Response.Flush();
    Context.Response.Write(strJSON);
}

and the following javascript (using mootools)

new Request.JSON({
url:<url to webservice>,
method: 'GET',
onSuccess: function(resJson, respText) {
        jsonResponse = resJson;
    dropJSON();
}
}).send();

the previous is a function for getting Googlemaps markers from an SQL server database in ASP.NET c#

like image 94
kolin Avatar answered Jan 02 '26 14:01

kolin