Issue: Cross-domain is not allowed by default in modern browsers.
Approach: cross-domain through JSONPin JS, it is not possible for us to request data from different domains directly with XMLHttpRequest. However, it is possible to introduce JS script files on different domains on the page, and JSONP is using this feature to achieve this. There are two sites: 1. The site to get the data: Http://localhost:1326/Default.aspx 1.1 page Get the JSON data for the page 2. To display the site that gets the results
Source page of the data source
protectedvoid Page_Load (object sender, EventArgs e)
{
//Summary : Gets or sets the HTTP character set for the output stream .
//Return result : System.Text.Encoding object that contains information about the character set of the current response.
response.contentencoding = Encoding. UTF8;
//Summary : Gets or sets the HTTP MIME type of the output stream . //
//Return result : The HTTP MIME type of the output stream . The default value is "text/html".
Response.ContentType = "Application/javascript";
String jsoncontent = "{' Innerdevid ':", ' Innerorgid ': +} ";
//Writes a string to the response output stream
/*
* The method defined here must be added window.onload. Because if not added, this code will be executed when the page is not loaded.
*/
Response.Write ("window.onload = function () {dosomething (" + jsoncontent + ")}");
}
Use the source code of the page
<%@pageLanguage= "C #"AutoEventWireup= "true"codebehind= "Default.aspx.cs" Inherits= "Testgetjson._default"%>
<! DOCTYPEhtml>
<htmlxmlns= "http://www.w3.org/1999/xhtml" >
<headrunat= "server" >
<title> test page </title>
<scripttype= "Text/javascript"src= "scripts/jquery-1.7.1.js" ></ Script>
<scripttype= "Text/javascript" >
function dosomething (jsondata) {
var odiv = document.getElementById (' div1 ');
var strjson = json.stringify (jsondata);
//$ ("#div1"). Text (Strjson);
Odiv.innerhtml=strjson;
returnfalse;
}
</Script>
<scripttype= "Text/javascript"src= "http://localhost:1326/Default.aspx" ></ script>
</head>
<body>
<formid= "form1"runat= "server" >
<P> here is the cross-domain issue of loading data from other Web pages or loading data from the server </P>
<divid= "div1" ></div>
<pid= "pmsg" ></P>
</form>
</body>
</html>
Using jquery for cross-domain
using jquery to achieve cross-domain reality is simple. As long as the service area back to the JSON data, plus the callback function name , it can be implemented.
Server-side modification, combined with jquery to implement cross-domain----modified implementation code
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using system.web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
namespace TESTJSOPGBK
{
PublicPartialclass_default : System.Web.UI. Page
{
protectedvoid Page_Load (object sender, EventArgs e)
{
//Summary : Gets or sets the HTTP character set for the output stream .
//Return result : System.Text.Encoding object that contains information about the character set of the current response.
//Exception : system.argumentnullexception: An attempt was made to set System.Web.HttpResponse.ContentEncoding to null.
response.contentencoding = Encoding. UTF8;
//Summary : Gets or sets the HTTP MIME type of the output stream .
//Return result : The HTTP MIME type of the output stream . The default value is "text/html".
Response.ContentType = "Application/javascript";
String jsoncontent = "{' Innerdevid ':", ' Innerorgid ': +} ";
//Summary : Gets the collection of HTTP query string variables.
// return results : A System.Collections.Specialized.NameValueCollection that contains a collection of query string variables sent by the client. For example, if requested: url for http://www.contoso.com/default.aspx?id= 44, the value of system.web.httprequest.querystring is
string dosomething = request.querystring["Callback"] = = null ? "Jsoncallback" : request.querystring["Callback"]. ToString ();
//Writes a string to the response output stream
/*
* This will write the method name from the URL dynamically before returning the data.
*/
Response.Write (dosomething + "(" + Jsoncontent + ")");
}
}
}
}
In jquery There are two types of solutions for cross-domain requests for Ajax, but only the get method is supported. the jquery.ajaxjsonp format and Jquery.getscript method of jquery are respectively .
Ajax in a variety of browsers to achieve the perfect cross-domain need to rely on JSONP technology,Jsonp is the essence of the request for a JS script file, when the JS file is loaded, execute a function, so you can perfect the rule cross-domain problem.
What is the jsonp format? API Original: If the obtained data file is stored on the remote server (the domain name is different, that is, cross-domain Fetch data), you need to use the JSONP type. With this type, a query string parameter callback= is created, and this parameter is appended to the requested URL. The server side should precede the JSON data with a callback function name in order to complete a valid JSONP request. This means that the remote server needs to do the processing of the returned data, and returns a callback (JSON) data based on the callback parameters submitted by the client, and the client will process the return data in a script way to JSON data to do the processing. Jquery.getjson also supports JSONP data-mode invocation.
Cross-domain-custom JSONP for cross-domain