Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning HTML from JSON webservice - what is the ".d"?

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

like image 326
Duncan Avatar asked Apr 11 '09 10:04

Duncan


2 Answers

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

like image 51
Magnar Avatar answered Oct 01 '22 19:10

Magnar


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

like image 25
Khb Avatar answered Oct 01 '22 18:10

Khb