Get JSON data with jQuery from a .NET service: confused with ajax setup

后端 未结 3 1074
北荒
北荒 2020-12-11 18:04

I\'ve just spent six hours trying to get this straight in my head and I haven\'t succeeded.

There\'s a HelloWorld .NET 3.5 web service on my local machine. Set up as

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-11 18:37

    I think you don't understand the jQuery specs: (Specs)

    dataType

    Default: Intelligent Guess (xml, json, script, or html)

    The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response...

    Now. Look at your various cases.

    Case 1: dataType: "json".

    You receive "xml" but declare "json" => you get a parseerror because you can't parse xml as if it was json.

    Case 2: dataType: "application/json".

    "application/json" is NOT a valid data type, so jQuery defaults to string.

    Case 3: No dataType.

    jQuery makes its best guess, which turns out fine in your case.

    Case 4: contentType: "application/json; charset=utf-8"

    You ask for json data, and you don't specify the dataType. In this case you are lucky the Webservice does return json data, and jQuery guesses correctly that the data are in json format.

    About the date formating, you want to look at:

    How do I format a Microsoft JSON date?

提交回复
热议问题