Cross-origin-use jsonp

Source: Internet
Author: User
Tags to domain

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.