0

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;

that was a webservice

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?

if the problem is something else, please tell me

thanks

1
  • @FreeAsInBeer where should I set that type please? i saw you my c# code, it is a json.net which is a library, Commented Apr 10, 2014 at 13:05

3 Answers 3

3

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();
Sign up to request clarification or add additional context in comments.

4 Comments

I can write response in my c# code in a web servcie asmx ? really ?
my function in c# should be void? or returing something ?
@user3432257 You should be able to write to the response by referring to it using its full namespace path, answer edited. You should be using void since you're writing directly to the response and not returning anything.
@user3432257 Yes, return void if you're writing directly to the response.
1

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" );
  });

4 Comments

should the success and done functions take the response as a parameter ?
yes, I copied this from the getJSON api doc page, generally you would accept the responses in success/done
your code looks good in your post, I think you're missing something happening in the parser (as FreeAsInBeer mentioned the ContentType) and it's being lost, with the .fail that would be exposed
my code had nothing to do w/ get/post, it was simply the callbacks for the getJSON method itself. Thanks for the +1. When I said "your post", I meant your question above :)
0

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/

1 Comment

@user3432257, Great,This might help sombody else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.