Detailed decryption Jsonp cross-domain request _javascript tips

Source: Internet
Author: User
Tags httpcontext script tag

1. What is cross-domain request:

A page on server A that requests a handler on Server B, which is called a cross-domain request

The test page for this time is:

Handler Kimhandler.ashx, as follows:

%@ WebHandler language= "C #" class= "Kimhandler"%>

using System;
Using System.Web;

public class Kimhandler:ihttphandler {public
  
  void ProcessRequest (HttpContext context)
  {
    string msg = ' {\ ' n Ame\ ": \ kim\", "gender\": \ "men \", "age\": ";
    Context. Response.Write (msg);
 
  public bool IsReusable {get
    {return
      false;
}}}

The other handler handler.ashx as follows:

<%@ WebHandler language= "C #" class= "Handler"%>

using System;
Using System.Web;

public class Handler:ihttphandler {public
  
  void ProcessRequest (HttpContext context)
  {
    string callbackname = Context. request.params["Callbackfun"];
    String msg = callbackname+ "({\" name\ ": \" kim\ ", \" age\ ": \" 18\ "});";
    Context. Response.Write (msg);
 
  public bool IsReusable {get
    {return
      false;

}}}

2.Ajax cannot implement Cross-domain request

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
  <title></title>
  <script>
    var requesturl =" http:// Qxw1192430265.my3w.com/kimhandler.ashx ";
    Window.onload = function () {
      document.getElementById ("Btnajax"). onclick = function () {
        var xhr = new xmlhttprequ EST ();
        Xhr.open ("Get", Requesturl, true);
        Xhr.setrequestheader ("If-modified-since", 0);
        Xhr.onreadystatechange = function () {
          if (xhr.readystate = = 4 && xhr.status =) {
            var res = Xhr.res Ponsetext;
            Alert (res);
          }
        Xhr.send (null);
      }
  </script>
 
 

View monitor, found no return of any request report style

3. Using the script tag, you can implement cross domain requests

The test code is as follows:

<! DOCTYPE html>
 
 

View monitor, you can see, there are return request report style

Look in the JSON format

4. Use JS mode , in the browser side, read the response is the data

Test code as follows, note that a handler has been changed

<! DOCTYPE html>
 
 

Through the background code, we can know

And watch it on the monitor.

Found in the browser side, pop up Kim and 18

5. Use JQ to implement Cross-domain requests (the internal principle is to create a script tag for us)

The code is as follows

 <! DOCTYPE html>  
 

Click on the button, you can see the effect, and then look at the monitor


The above mentioned is the entire content of this article, I hope you can enjoy.

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.