views:

1524

answers:

2

Hi

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... ?

+11  A: 

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

Magnar
Hmmm when I visit the URL directly I actually get XML - with <string> as it's root element. But it does work in code! I've also marked the service function with [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
Duncan
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
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
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
Brilliant, thanks!
Duncan
+3  A: 

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
Thanks - I can't mark two answers but am grateful for your help.
Duncan
Thank you Duncan
Khb