Solving Ajax Cross-domain Problems

Source: Internet
Author: User


Scenario: In the project I am responsible for background interface development, which is provided to developers on the app side. Ajax Cross-domain problem occurs when the app side AJAX remote request server. Since I was first developed and never met before. So I went online to check the post, and finally know the way to achieve. Although not very understand the principle. But also solve this problem, so record, to facilitate their later review.


Idea: Ajax request backstage, in the datatype attribute write ' Jsonp ', then add a new attribute: Jsonp, property value is any character (such as Jsonpcallback), in the background

You can use this character name to receive a value that is used when the background data is returned. Because the format of returning data with JSONP is not the same as normal return. JSONP return data format is to return a format similar to the JS function but not {}, and you want to return the data is placed in the parameters, the specific format is: function name (background to transfer data), where the function name is ' Jsonpcallback ' received the actual parameter, the background transmits the data is oneself to return the data, for instance AJAX request, backstage receives ' Jsonpcallback ' the value is "the 1223541_js", You backstage to return your data is a collection called DataList. Then you can splice the background into the above format and eventually return to this format: 1223541_js (' dataList '), on it.


Note: Why ① the function name of the return function using the value of the incoming background parameter of the Jsonp property. : Because each AJAX generates a different random character and returns a value with random characters, you can find out which Ajax request it is.

The ② () is the parameter to return, but the single quotation mark must be spliced so that it is returned as a variable parameter.


The foreground AJAX format is shown in the figure:


The above is the foreground of the request format, in fact, and ordinary Ajax no difference, that is, datatype value into a JSONP, and then a number of attributes: Jsonp property values can be casually written. When Ajax requests the background, the background gets the real value based on the string name in the JSONP and then the concatenation is returned. The value returned is in the parameter of the callback function. Then you can do the same as normal AJAX operations.

Background code:

The code is not all there are many annotations that are not added but do not affect

  The method that is entered when the public class testjsonpaction{//foreground request is operated public
  String Getlogisticsstatuslist () {

  String jsonpcallback = getparameter ("Jsonpcallback");//receive the parameters of the AJAX request.

  .../other data received and other processing code

  list<map> result = Page.getresult ();//Background The data String to be returned is
  resultdate = jsonobject.fromobject (results). toString ()///Convert data to JSON format (project required)

  ///As the stitching part is written as a tool class, called here. (Tool class code below)
  String callbackdata= jsonutil.getjsoncallbackstring (resultdate, jsonpcallback); Invoke tool class stitching into jspon required format

  
    Struts2utils.renderjson (callbackdata);//Final return to foreground data

  }
}

The tool class in the stitching format:

Package com.nebula.app.app.common.utils;

public class Jsonutil {
		
	/**
	 * @param str to return the data from the foreground * * * * * 
	 @param The value received by the Jsonpcallback reception
	 * 
	 * * * * * * * * * * Public
	static String getjsoncallbackstring (String  str, string jsonpcallback) {
		
		if jsonpcallback = null | | '. Equals (Jsonpcallback)) {return
			str;
		} else{return
			jsonpcallback+ "(' +str+" ') ';}}}
  


Great God's blog has a professional introduction

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.