Reputation: 415
I have a datatabe and I want to change it to json. I did that using json.net lib like this
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public object LoadTotalCallsPerCampignByRangeFilter(string fromDate, string toDate)
DataTable DTgraph = new DataTable();
....
....
string jsonformatstring = JsonConvert.SerializeObject(DTgraph, Formatting.Indented);
return jsonformatstring;
then i tried to consuem it from jquery like this:
$.getJSON('http://localhost:4025/vmp_webservice.asmx/LoadTotalCallsPerCampignByRangeFilter',
{ fromDate: "01-01-2014", toDate: "09-04-2014" } )
.done(function (result) {
alert("hi");
});
I check the chrome debugging tool (f12) and I can see the request and the data returned, but the alert function is never fired.
this is the data returned.
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">[
{
"Campaign": "default",
"TotalInBound": 216.0
},
{
"Campaign": "direct",
"TotalInBound": 10.0
},
{
"Campaign": "Sales",
"TotalInBound": 151.0
},
{
"Campaign": "Support",
"TotalInBound": 2.0
}
]</string>
I see the problem that in my web service, it is returning string formated
not json object
so how can I return object json not string formated json?
thanks
Upvotes: 0
Views: 2108
Reputation: 1309
Webservise Will return string because ,before the return statement you are serializing to string and sending to the client.
string jsonformatstring = JsonConvert.SerializeObject(DTgraph, Formatting.Indented);
So in your consuming app you have to deserialize back to object, To see the object format.
https://api.jquery.com/jQuery.parseJSON/
Upvotes: 0
Reputation: 12979
If you're wanting your response to be seen as JSON, you need to set your ContentType:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
HttpContext.Current.Response.Write(jsonformatstring);
HttpContext.Current.Response.End();
Upvotes: 3
Reputation: 8166
try using the done, always and fail methods for debugging. My guess is it's trying to parse the string as JSON and failing. You can replace the console.log's below with alert
.done(function() {
console.log( "second success" );
})
.fail(function( jqxhr, textStatus, error ) {
console.log( error );
})
.always(function() {
console.log( "complete" );
});
Upvotes: 1