receiving response from page method using jquery
I am calling a page method using jQuery. In examples, every one receives a reply 开发者_如何学运维using result.d
(.d)
For example:
function onSuccess(msg){
alert("suc" + msg.d);
}
Please guide me, what is the .d
?
From Dave Ward's blog:
If you aren’t familiar with the “.d” I’m referring to, it is simply a security feature that Microsoft added in ASP.NET 3.5’s version of ASP.NET AJAX. By encapsulating the JSON response within a parent object, the framework helps protect against a particularly nasty XSS vulnerability.
For complete write up head to this post.
d is a property of msg.
example
var msg = {
d: 'foo'
}
//echoes 'foo'
alert(msg.d);
well that means that msg
is an Object
and you are accessing the d
element
An object can look like this:
var msg = {
d: "hello",
e: "there"
}
alert(msg.d);//alerts 'hello'
alert(msg.e);//alerts 'there'
Assuming your doing an Ajax call here.. And in your AJAX call you've set dataType: 'json'..
So, Suppose you did something like this - I'll use PHP as an example language...
$var = array('d' => "Hello"); echo json_encode($var);
Then in your client you could do something like:
onSuccess(msg) { alert(msg.d); // Would alert "Hello" }
In Javascript . just references object members..
do var blah = {d: "Hello" } ; console.log(blah.d); would output "Hello"..
".d" is (or should be) a "property" of the json object recieved as the msg.
javascript object notation (json) serializes the object at the end of the webmethod in a way that javascript will inspect the object (in this case, msg) and look for properties using the traditional "object.property" notation.
Your response data is a JSON Object which contains a property/key
called d
. Usually ajax responses are encoded in JSON format so that it can be accessed from the javascript at the client side.
In asp.net, to encode a response in JSON format, you generally use JSON.NET framework.
The JSON response that you are receiving from the server contains a key/property named d
, which you are accessing by writing msg.d
.
Typically a JSON object has the following structure -
var jsonObject = {
key1: value_1,
key2: valuu_2,
.............
keyn: value_n
}
and then you access a key/property value in the following way -
jsonObject.key1
Here, value
can be any javascript datatype, i.e., strings, numbers, even other JSON objects.
the field 'd' is the name of a property on the response data, i think this could be named 'd', to reference the 'data' of the response, but if you want to know more about this, you could check the jquery file and look for the ajax method.
精彩评论