8

This is one of those situations where I've had to pick up and run with a new tech without having time to learn the foundations!

I have the following js function which calls out to PrintService, which returns me the HTML to inject into a div:

function showPrintDialog() {
  $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: "{}",
        dataType: "json",
        url: "http://localhost/PrintService/PrintService.asmx/RenderPrintDialog",

        success: function(data) {
            $("#printdialoginner").html(data.d);

I struggled with this FOR AN AGE before I noticed the ".d" in another example

So, it works - but why? What is this ".d" ?

Apologies if this is a noob question, but google is not being my friend here.

Thanks

Edit: Magnar is right, it is a .NET specific thing. Check out Rick Strahl here - http://www.west-wind.com/weblog/posts/164419.aspx

What confuses me is that it MUST return JSON as my client script code is quite happy about the return, but when I access the browser I get XML... ?

Mickael Lherminez
  • 679
  • 1
  • 10
  • 29
Duncan
  • 10,218
  • 14
  • 64
  • 96
  • 1
    You are asking for JSON in your ajax-call, and the web service framework seems to support that. When you access the URL through the browser, you are not specifically asking for JSON, and thus get the default XML. – Magnar Apr 11 '09 at 11:56
  • Also, if you want to see the response from the server to the actual ajax-call, you can do so using the Console-panel of Firebug. Just expand the POST-call in the log and look at the Response-tab. – Magnar Apr 11 '09 at 12:04
  • You may find this article of use - http://encosia.com/2009/02/10/a-breaking-change-between-versions-of-aspnet-ajax/ – Russ Cam Apr 11 '09 at 12:30
  • related: http://stackoverflow.com/questions/830112/what-does-d-in-json-mean – Ruben Bartelink Sep 21 '12 at 01:42

2 Answers2

15

The PrintService responds with JSON, a data transfer format based on the JavaScript Object Notation. So the data-parameter is an object, not an HTML-string. This object seems to have a member called d, containing the HTML.

If you visit the URL directly http://localhost/PrintService/PrintService.asmx/RenderPrintDialog, you should see the following:

{
    d: "<html here>"
}

with possibly other members aswell.

The curly brackets denote an object, and inside are key: value pairs delimited by commas. You can read more about json at json.org.

Exactly why it's called d is something you'll have to take up with the author of the PrintService. ;-) Maybe markup or html would be a more helpful name.

Edit

It turns out that Duncan is the author of the PrintService, and did not himself include the 'd'. Also, when visiting the URL he sees XML, not JSON. The .NET framework for web services in use responds with JSON when asked for it in the http request. The notorious d-member is added as a wrapper by that framework, in order to prevent cross site scripting.

This article explains the whole deal: A breaking change between versions of ASP.NET AJAX

iokevins
  • 1,427
  • 2
  • 19
  • 29
Magnar
  • 28,550
  • 8
  • 60
  • 65
  • Hmmm when I visit the URL directly I actually get XML - with as it's root element. But it does work in code! I've also marked the service function with [ScriptMethod(ResponseFormat = ResponseFormat.Json)] – Duncan Apr 11 '09 at 11:20
  • BTW I wrote the PrintService and never introduced ".d" anywhere - that is why it is such a mystery! But I see it coming up in other places where the service has been created in .NET? – Duncan Apr 11 '09 at 11:27
  • Seems like I missed the mark completly then. I can't see any magic 'd's in the jQuery code. I'm guessing that the service responds with JSON if you ask for it in the http-request, and that the 'd' member is .NET specific. – Magnar Apr 11 '09 at 11:31
  • Yeah, I think it's a .NET thing :( Thanks anyway - I might rephrase the question in future to draw attention to it as being a .NET-ism! – Duncan Apr 11 '09 at 11:34
5

ASP.Net nests the JSON data in the d property because of cross site scripting attacks.

It is possible to return script code as the JSON response, and nesting the data inside the .d property makes it unparsable to the browser.

See here: JSON vulnerability

Regards K

Khb
  • 1,423
  • 9
  • 9