Cross-domain access JS implementation.
Environment:. net3.5+jquery+json.net
Because in the cross-domain implementation, so new site here, this site only need:
(1) Customer class
public class Customer
{
public int Unid {get; set;}
public string CustomerName {get; set;}
public string Memo {get; set;}
public string Other {get; set;}
}
(2) ashx file
Customer customer = new Customer
{Unid = 1, CustomerName = "Song Jiang", Memo = "Day Kui xing", other = "Black Three Lang"};
String Strjson = Newtonsoft.Json.JsonConvert.SerializeObject (customer);
Context. Response.Write (Strjson);
Then build the Web site in IIS Web1, which is the site above.
Re-build the Web site project WEB2 in vs for cross-domain access to Web1.
(i) normal JQUERYAAJX cross-domain requests
function Jsonpajax_2 () {
$.ajax ({
URL: "Http://192.168.1.105:8123/Handler.ashx",
Type: "Get",
DataType: "JSON",
Success:function (data) {
var tt = ';
$.each (data, function (k, v) {
tt + = k + ":" + V + "<br/>";
});
$ ("#divmessage"). HTML (TT);
}
});
}
Result: A prompt window will pop up.
(ii) Jquery JSONP request
function Jsonpajax_1 () {
$.ajax ({
URL: "http://192.168.1.105:8123/Handler.ashx?callback=?",
Type: "Get",
DataType: "Jsonp",
JSONP: "Callback",
Success:function (data) {
var tt = ';
$.each (data, function (k, v) {
tt + = k + ":" + V + "<br/>";
});
$ ("#divmessage"). HTML (TT);
}
});
}
At this point, the ashx file data in the cross-domain Web1 is provided to change:
String callback = Context. Request.params["Callback"];
Context. Response.Write (callback+ "(" +strjson+ ")");
The format of the returned data is:
jsonp1263199953609 ({"Unid": 1, "CustomerName": "Song Jiang", "Memo": "Tian Kui xing", "other": "Black Three Lang"})
Results:
Unid:1
CustomerName: Song Jiang
Memo: Tian Kui xing
Other: Black Tri lang
Quote a Description:
The most basic principle of JSONP is: adding a <script> tag dynamically, and the SRC attribute of the script tag is not a cross-domain limitation. In this way, this cross-domain approach is actually unrelated to the Ajax XMLHttpRequest protocol.
In fact, "jquery Ajax cross-domain problem" has become a pseudo-proposition, jquery $.ajax method name is misleading.
If set to datatype: ' Jsonp ', this $.ajax method has nothing to do with Ajax XMLHttpRequest, instead it is the JSONP protocol.
JSONP is an unofficial protocol that allows the server-side integration of script tags back to the client to achieve cross-domain access via JavaScript callback
JSONP is the JSON with Padding. Due to the limitations of the same-origin policy, XMLHttpRequest only allows resources to be requested for the current source (domain name, protocol, port). If you want to make cross-domain requests,
We can make cross-domain requests by using the HTML script tag and return the script code to execute in the response, where you can pass the JavaScript object directly using JSON.
This cross-domain communication method is called JSONP.
Jsoncallback function jsonp1236827957501 (...): A callback function that is registered by the browser client and gets the JSON data on the cross-domain server
Jsonp principle:
First register a callback (for example: ' Jsoncallback ') on the client, and then pass callback's name (for example: jsonp1236827957501) to the server.
At this point, the server becomes JSON data.
Then, in JavaScript syntax, a function is generated, and the function name is the value jsonp1236827957501 of the parameter ' Jsoncallback ' passed up.
Finally, the JSON data is placed directly into the function in the form of a parameter, so that a document of JS syntax is generated and returned to the client.
The client browser, parses the script tag, and executes the returned JavaScript document, at which time the JavaScript document data, as parameters,
Passed into the client's pre-defined callback function (as in the previous example, the jquery $.ajax () method encapsulates the Success:function (JSON)). (Dynamic execution callback function)
Can say Jsonp way principle and <script src= "//cross-domain/...xx.js" ></script> is consistent (QQ space is a large number of this way to achieve cross-domain data exchange). JSONP is a script injection (scripts injection) behavior, so there are some security implications.
Transferred from: http://www.cnblogs.com/jams742003/
Cross-domain access to jquery implementation [GO]