9

I'm trying to use jQuery and JSON with a C# Web Service that I wrote. No matter what, the following code will only output in XML.

Webservice Code

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld() {
    return "Hello World!";
}

I also have these attributes assigned to the class

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]

jQuery Code

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "ScheduleComputerDS.asmx/HelloWorld",
    data: "{}",
    dataType: "jsonp",
    success: function(data) {
        alert(data);
    }
});

The ASMX page always returns as content type "text/xml". Anything I'm missing?

EDITS: In response to a couple answers:

If I have the datatype as just "json" the content is still XML and jQuery also will not call my callback function. If I add the "&callback=?" to the url, IIS throws a HTTP 500 error.

My class does inherit from "System.Web.Services.WebService".

From doing some research on your guys answers, it looks like I do need to mess with WCF. Unfortunately the JSON that is returned is more designed for MS Ajax and is a lot of useless bloat for my use. I may look into an open source library like Jayrock or something similar.

Thanks for all your help!

3
  • Im new to Stackoverflow so I dont know how this comment is going to look in reference to the others but... If I have the datatype as just "json" the content is still XML and jQuery also will not call my callback function. If I add the "&callback=?" to the url, IIS throws a HTTP 500 error. Commented Mar 19, 2009 at 20:13
  • @TheDude - you can edit your original question to add this information in. There's an edit link under the tags on the bottom left of the question section Commented Mar 19, 2009 at 20:14
  • If you can use the WCF REST Starter Kit, just released a Preview 2 Commented Mar 19, 2009 at 20:15

4 Answers 4

6

I think there's a typo:

dataType: "jsonp",

Should be:

dataType: "json",
Sign up to request clarification or add additional context in comments.

3 Comments

awesome :) i made this mistake recently but i had "script" in there. was copied and pasted from an example
I know its an old post, I just wanna make sure that everyone new on this, dont fall on the wrong way, its jsonp no typo there, check json-p.org
@LuisSánchez: jsonp is not recommended. leave it json. directly from that link you provided: "authors who rely on JSON-P for cross-domain Ajax are in fact opening themselves up to potentially just as much mayhem as was attempted to be avoided by implementing the same-origin policy in the first place"
3

As far as I know, the ScriptService attribute just allows the service to automatically create a JavaScript proxy (by appending /js to the endpoint address - ScheduleComputerDS.asmx/js in your case). It does not allow you to call the operations on the service the way you're trying to do.

You could instead use a RESTful WCF service (which requires .NET 3.5) which you can access by sending a properly shaped URI via an HTTP GET.

Comments

3

Rich Strahl has a really basic post that should help you out with this.

http://www.west-wind.com/weblog/posts/164419.aspx

1 Comment

This link was very helpful. Thank you
1

Have you tried with datatype json?

Also, have a look at Encosia's Using jQuery to Consume ASP.NET JSON Web Services article on the matter. There's some good info on common pitfalls too.

Comments

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.