ArticleDirectory:
Cross-origin-Knowledge
Cross-origin-use jsonp
Cross-origin-use JS files
Cross-origin-Use window. Name
Cross-origin-use proxy page or cross frame
This is unidirectional cross-origin. The implementation method is to request data from Domain A to Domain B, and Domain B generates jsonp data to. We use jquery for communication.
Steps:
1. Domain A frontend call page
<% @ Page Language = " C # " Autoeventwireup = " True " Codebehind = " Default. aspx. CS " Inherits = " Webapplication1. _ default " %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< Html Xmlns = "Http://www.w3.org/1999/xhtml" >
< Head Runat = "Server" >
< Title > No title page </ Title >
< Script SRC = "./JS/jquery.1.3.2.js" Type = "Text/JavaScript" > </ Script >
< Script Type = "Text/JavaScript" >
Function Getdata ()
{
VaR URL = " Http:// B .5173.com/Default.aspx " ;
$. Ajax ({
URL: URL,
Datatype: " Jsonp " ,
Success: Function (JSON ){
Alert (JSON. msg );
},
Error: Function (){
Alert ( " Error " );
}
})
}
</ Script >
</ Head >
< Body >
< Form ID = "Form1" Runat = "Server" >
< Input Type = "Button" Value = "Cross-origin call" Onclick = "Getdata ();" />
</ Form >
</ Body >
</ Html >
2. Domain B mainly generates jsonp, so only the backgroundCode.
Using System;
Using System. Data;
Using System. configuration;
Using System. collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. Web. UI. htmlcontrols;
Namespace Webapplication2
{
Public Partial Class _ Default: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{
Response. charset = " UTF-8 " ;
Response. contenttype = " Text/JavaScript " ;
String Callback = request. querystring [" Callback " ];
String JSON = " {'State': '0', 'msg ': 'Hello world! '} " ;
String Result = String . Format ( " {0} ({1 }) " , Callback, JSON );
Response. Write (result );
Response. Flush ();
Response. End ();
}
}
}
Call result:
Reference: http://www.cnblogs.com/hb_cattle/archive/2011/10/27/1713659.html