Jsonp for cross-domain access

Source: Internet
Author: User

To achieve JSONP cross-domain access, you first need to understand what is cross-domain? Then jsonp the relationship with JSON?

1. What is cross-domain?

Cross-domain is simply a domain name under the program and another domain under the program to do data interaction. For example: There is a http://www.zq.com and http://www.qt.com two separate sites, to achieve from the latter site forward to a site to fetch data. As a data source as a server, the access to data as a client, to achieve service-oriented programming.

One of the most important items in a Dynamic Web page is fetching data. If it is in the same project, the site is hung under the same domain name, in this case both the foreground Ajax or the background directly fetch data is very convenient. But if like the two sites above, under different domain names, then fetching data becomes very troublesome (now has Web servers and WCF implement service-oriented programming)

2, under what circumstances do cross-domain access with JSONP?

When you want to use AJAX to fetch data from other projects. If you are fetching data from other sites in the background, you can do so using techniques such as Web servers or WCF.

3. The relationship between JSONP and JSON?

JSON is a text-based way of interacting with data, and JSONP is based on the way JSON is used. From the use of simple to say: Do not cross-domain with JSON to achieve a Jax and background data interaction, cross-domain with JSONP implementation of Ajax and other domains of the program to do data interaction.

4, $.ajax realize JSONP

[HTML]View Plaincopy < param name= "wmode" value= "Transparent" >
  1. <%@ page language="C #" autoeventwireup="true" codebehind="PRequest.aspx.cs" Inherits ="jsonp.prequest"%>
  2. <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
  3. <HTML xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <script src="scripts/jquery-1.7.1.min.js" type="Text/javascript"> </Script>
  6. <script src="scripts/json2.js" type="Text/javascript"></script >
  7. <script type="Text/javascript">
  8. function aa () {
  9. var person = {};//custom array
  10. person.name = "AName";
  11. person.age = 18;
  12. Console.log (json.stringify (person));
  13. $.ajax ({
  14. URL: "Http://localhost:58878/jp.ashx",//program in another domain
  15. Type: "Get",
  16. DataType: "Jsonp",//The expected return type is JSONP
  17. Data: {' person ': json.stringify (Person)},
  18. JSONP: "Callback",//overriding the name of the callback function in a JSONP request, can save
  19. Success:function (RT) {
  20. var result = json.parse (RT);
  21. Console.log (result);
  22. $ ("#lb"). HTML (result. Name + "<br />" + result.  Age);
  23. }, Error:function (e) {
  24. Console.log (e);
  25. }
  26. });
  27. }
  28. </Script>
  29. </head>
  30. <body>
  31.         <input < span class= "attribute" >id= "Button1"  type= "button"  value= "value"  onclick= "AA ()"  />&NBSP;&NBSP;
  32. <div id = "lb" style="width:90px;height:90px;background-color:red"></ div>
  33. </body>
  34. </html>


It is not difficult to find out how it differs from the JSON notation:

1, the subtle differences in the URL, in the Jsonk is often directly under a path of the processing program, and JSONP is written in other sites (domain) processing Code program path. But note that JSON can also be written like this

2. Datatypy is now dealing with JSONP, not JSON.

Other data, parameters, etc., and JSON do not have two different

Data Source Section

[CSharp]View Plaincopy
  1. Using System.Web;
  2. Namespace Presponse
  3. {
  4. // <summary>
  5. // Summary description for JP
  6. // </summary>
  7. public class Jp:ihttphandler
  8. {
  9. public class person
  10. {
  11. public string Name { get;  set;}
  12. public int Age { get;  set;}
  13. }
  14. public void ProcessRequest (HttpContext context)
  15. {
  16. Context.  Response.ContentType = "Text/plain";
  17. String callback = context. request["Callback"]; Callback not less
  18. String CA = context.  request["Person"];
  19. Person B = newtonsoft.json.jsonconvert.deserializeobject<person> (CA);
  20. string response = "{\" name\ ": \" "+ b.name + " \ ", \" age\ ": \" "+ b.age + " \ "}";
  21. string re = Newtonsoft.Json.JsonConvert.SerializeObject (response);
  22. String call = Callback + "(" + Re + ")"; Response and re are the same, the front callback cannot be less
  23. Context. Response.Write (call);
  24. }
  25. public bool IsReusable
  26. {
  27. Get
  28. {
  29. return false;
  30. }
  31. }
  32. }
  33. }

This enables cross-domain access

5. Description

1) The reason to collect data in the foreground is JSON and JSONP, because JavaScript's homologous strategy

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.