JSONP cross-domain access services

Source: Internet
Author: User

Some time ago in the product development, need to interact with the company's Web site, we developed the product is placed in a domain name, the company's website that block is under another domain name, so in the page call

Site over the interface there is a cross-domain problem, at that time in order not to modify the interface over the site, so the use of the server through the WebService way to invoke the site interface, the problem is quickly resolved, then I was thinking

If you need to directly access in JS, it involves cross-domain issues, then how to solve this problem, I went online to find some information, and finally adopted the JSONP way to solve. Let me share with you the following

Solve cross-domain problems in a jsonp way. Divided into the following steps:

1, introduce jquery.js, use $.ajax () to define JSONP, as follows:

<%@ page language= "java"  contenttype= "text/html;  Charset=utf-8 " pageencoding=" Utf-8 " %><%string path = request.getcontextpath (); %><! doctype html public  "-//w3c//dtd xhtml 1.0 transitional//en"   "HTTP// Www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

2, server-side processing:

package com.mall.web.action;import java.io.ioexception;import java.io.printwriter;import  java.util.hashmap;import java.util.map;import javax.servlet.http.httpservletrequest;import  javax.servlet.http.httpservletresponse;import org.springframework.stereotype.controller;import  org.springframework.web.bind.annotation.modelattribute;import  org.springframework.web.bind.annotation.requestmapping;import  Org.springframework.web.bind.annotation.requestmethod;import org.springframework.web.servlet.modelandview ;import com.alibaba.fastjson.jsonobject; @Controller @requestmapping ("/demo/") public class  demoaction {private httpservletrequest request;private httpservletresponse response;@ Modelattributepublic void setrequestandresponse (httpservletrequest request,httpservletresponse  response) {this.request = request;this.response = response;} @RequestMapping (value= "Jsonp", Method=requestmethod.get) Public void jsonp () { try {           response.setcontenttype ("Text/plain");           response.setheader ("Pragma",  "No-cache");           response.setheader ("Cache-control",  "No-cache");           response.setdateheader ("Expires",  0);           printwriter out = response.getwriter ();                jsonobject resultjson = new jsonobject ();         resultjson.put ("name",  "Wen");         resultjson.put ("Age",  ";        ") string jsonpcallback = request.getparameter ("Jsonpcallback");//Client request Parameters            system.out.println ("-------------------->" +jsonpcallback);         system.out.println ("-------------------->" +resultjson.tojsonstring ());         out.println (jsonpcallback+ "(" +resultjson.tojsonstring () + ")");//return JSONP format data            out.flush ();           out.close ();        } catch  (IOException e)   {         e.printstacktrace ();         }  }}

The URL address for this visit is:/demo/jsonp.action? jsonpcallback= "Showage"

In the above examples, the following points need to be noted:

1. Only get requests can be used in Jsonp, datatype is JSONP in AJAX request

2. The parameter name in the URL address defined by JSONP defaults to callback

3, Jsonpcallback defines the value of the JSONP-defined parameter, and the server needs a callback function, if jsonpcallback to define the value, the default callback success function

4. After the server accepts the request, it needs to return the callback function passed by the parameter as follows:

Out.println (jsonpcallback+ "(" +resultjson.tojsonstring () + ")");//return JSONP format data


The above is JSONP's visit process, here need everybody to understand well. Because using JSONP cross-domain access requires that the client and server define a rule, the rule for the callback function, so

In the development process, the client and server developers are required to define a rule together.

In fact, we can also through the $.get () and $.getjson () way to cross-domain access, here is not to tell you, if interested friends, you can get to know the next, today to share this to you,

We'll see you next time, and then we'll share some of Android's knowledge.


This article is from the "technology endless, learning needs more Brain" blog, please be sure to keep this source http://wenqiangv.blog.51cto.com/7889138/1433339

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.