Solve Ajax cross-origin problems

Source: Internet
Author: User

SlaveAjaxSince its birth,XMLHttpRequestCross-origin requests are not allowed for objects. This seems to be a classic problem. Because of JavaScriptSame-origin policy(I will not discuss it in depth here.

There are several solutions:

1. Intermediate Layer Transition (can be understood as "proxy "):

The intermediate transition, obviously, is to add a transitional layer between Ajax and servers in different domains for communication, this layer transition can be PHP, JSP, C ++, and other languages with network communication functions. The middle layer reads data to servers in different domains. Take Asp.net as an example. If you need to communicate with an Asp.net in different domains, the XMLHttpRequest on the client first queries an Asp.net in the current domain, then, the Asp.net in the current domain will communicate with Asp.net in different domains, and then the Asp.net response output (response) in the current domain will be like this, I believe that the reader has clearly understood it.

2. Use the <SCRIPT> label

This method uses the src in the <SCRIPT> tag to query An ASPX to obtain the response, because the src attribute of the <SCRIPT> tag does not have a cross-origin issue.

Let's take a look at the following example:

<! 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 "> 

Getdemo. aspx background code:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace LearnJS{    public partial class getDemo : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            Response.Write("var responseVal='Dylan'");        }    }}

This method is also called ajaj or Ajax without XMLHttpRequest. It replaces X with J Because XML and XMLHttpRequest are not used when the <SCRIPT> tag is used. This method seems a bit "alternative", haha.

Now let's take a look at how to solve the cross-origin problem of Ajax with jquery:

<HTML> 

The content in the remote server js.txt is:
VaR Dylan = {name: "Dylan", email: Dylan@163.com}

I think this method is more concise. Haha. Of course, you can choose any implementation method based on the actual situation.

I have seen many people reluctant to face up to the technical bottlenecks of Ajax. In fact, Ajax should be more Ajax than Ajax, highlight the first A is to emphasize that AJAX is actually a method of asynchronous transmission, rather than the specific technology used in the end.

In factJSONAfter the data format, there is a better"Jsonp", Or cross-origin Ajax communication. In fact, jsonp is not a data format, but the second method I introduced has been improved. Since jquery1.2, jquery has local support for jsonp callback.

Due to the time relationship, I will not explain it in detail here. Interested readers can refer to the http://www.ibm.com/developerworks/cn/web/wa-aj-jsonp1/

This article is from blog.csdn.net/dinglang_2009.

Related Article

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.