Cross Domain JSONP ( Json with padding ) with Jquery and Servlet or JAX-WS
来源:互联网 发布:ubuntu traceroute6 编辑:程序博客网 时间:2024/05/21 02:20
Wednesday, May 30, 2012
Cross Domain JSONP ( Json with padding ) with Jquery and Servlet or JAX-WS
- Solving Cross Domain problem using JSONP ( Json with padding ) with Jquery and Servlet JAX-WS
- or Cross-domain communications with JSONP
- or Cross domain jquery or cross domain Ajax
- or java - Sending JSONP vs. JSON data
Well there are several techniques to address cross domain problem. Here are few.
Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: http://test.com:8080 http://foobar.com
The asterisk permits scripts hosted on any site to load your resources; the space-delimited lists limits access to scripts hosted on the listed servers.
Problem is CORS may not work in all browsers ( See: http://en.wikipedia.org/wiki/Cross-origin_resource_sharing)
2. Other option is to use some server side re-direct of the request to a servlet or a php script on your own domain which in trun calls third party web site.
But at times we may not have this option to implment a servlet or PHP script which we can call on our domain.
3.If the third party url that you are invoking supports JSONP response then all browsers work without complaining
What is JSONP ?
- JSONP - JSon with padding. It means to your JSON response we will append the callback method name.
eg: Let say your JSON response is {"totalInterestPmtAmt":5092.79,"totalPmtAmt":15092.79}
and lets assume the callback method name that was sent in request was getPayment JSONP Response will be :getPayment( {"totalInterestPmtAmt":5092.79,"totalPmtAmt":15092.79} )
(However if you dont give call back method name Jquery dynamically generates a method name sends in request and when response comes back it will use that method name to call it ...Read further everything will make sense...)
Access-Control-Allow-Origin: http://test.com:8080 http://foobar.com
The asterisk permits scripts hosted on any site to load your resources; the space-delimited lists limits access to scripts hosted on the listed servers.
Problem is CORS may not work in all browsers ( See: http://en.wikipedia.org/wiki/Cross-origin_resource_sharing)
2. Other option is to use some server side re-direct of the request to a servlet or a php script on your own domain which in trun calls third party web site.
But at times we may not have this option to implment a servlet or PHP script which we can call on our domain.
3.If the third party url that you are invoking supports JSONP response then all browsers work without complaining
What is JSONP ?
- JSONP - JSon with padding. It means to your JSON response we will append the callback method name.
eg: Let say your JSON response is {"totalInterestPmtAmt":5092.79,"totalPmtAmt":15092.79}
and lets assume the callback method name that was sent in request was getPayment JSONP Response will be :getPayment( {"totalInterestPmtAmt":5092.79,"totalPmtAmt":15092.79} )
(However if you dont give call back method name Jquery dynamically generates a method name sends in request and when response comes back it will use that method name to call it ...Read further everything will make sense...)
Since all browsers allow injecting java scripts from third party websites without complaining (that they are cross domain) in JSONP the response we need, is wrapped as a Java script method there by fooling the browser to think its as java script.
<script> tag is less restrictive hence in JSONP you got the same JSON response with a call back method added to it. So browser was happy that it was just injecting some java script function and did not complain. Sweet !!!
Enabling JSONP Server side using Servlet as wrapper for JAX-WS webservice
--------------------------------------------------------------------------------------------------
I had an exisitng JAX-WS webservice which returns JSON response. Most time this webservice is invoked by some other server component. But we had a client who had to call using Ajax and browser started complaining that we are accessing cross domain url.
Let say my webservice url was:
http://abcd.com/Calculation/CalculationWebServiceImpl.wsdl
And the caller is on
http://xyz.com/displayRate.html ---> this page has ajax call. (will show later )
So all we did was added a servlet called CalulationServlet which had the URL
http://abcd.com/Calculation/CalculationServlet
//SERVLET:
NOTE: Its very important that the response content type is set to text/javascript
Now I edited the web.xml that was already there for my webservice I added the following entry
Now my servlet can be invoked using the url
http://abcd.com/Calculation/CalculationServlet?amount=10000 (as the web app name is Calculation)
JQuery to invoke:
Inside the any html page assuming you have Jquery included
//Add some html and a button to call function callWebService( ) you should be all set.
so this work around will save you from CROSS Domain Issues.
0 0
- Cross Domain JSONP ( Json with padding ) with Jquery and Servlet or JAX-WS
- Accessing cross-domain services with LiveCycle Data Services or BlazeDS
- Qt WebView中出现cross domain ajax querying with jquery
- Jquery Cross-Domain ajax call using JSONP
- JAX-RS (Jersey) custom exception with XML or JSON
- error:Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and op
- Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option t
- Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option
- Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option t
- Connecting the dots with jQuery, JSONP, and WebAPI
- JQuery and Ext 4 Cross Domain sample
- jquery中的jsonp使用(cross-domain的一个方法)
- Using implicit SOAP headers with JAX-WS web services
- Publishing a RESTful Web Service with JAX-WS
- WCF 4.0 service consumed in Silverlight 4.0 with cross domain
- javascript: Jquery each loop with json array or object
- javascript: Jquery each loop with json array or object
- 100%height with padding and margin
- c的一些基本语法
- 64位 Linux 编译32位应用程序
- Thinking in java -- 压缩(GZIP与ZIP格式压缩)
- jpx编辑
- 使用StreamingKit 静态库,app崩溃
- Cross Domain JSONP ( Json with padding ) with Jquery and Servlet or JAX-WS
- 教你透彻了解红黑树
- 基于TCP的Socket编程
- Linux下rz,sz与ssh的配合使用
- 开篇
- maven+springMVC+mybatis+junit详细搭建过程
- iOS定时通知
- 一个好用的基于FireFox的网站更新自动提醒插件
- 使用ActionMode实现有删除动画的多选删除功能