开发者社区> jerrywangsap> 正文

在nodejs服务器和ABAP服务器上使用jsonp

简介: 在nodejs服务器和ABAP服务器上使用jsonp
+关注继续查看

In my blog Cross domain request in ABAP and Java with two workaround I introduce the step how to deal with Cross Domain issue using Cross-origin resource sharing ( CORS ) supported by almost all modern browsers.


And there is another alternative for cross domain issue, that is JSONP which can work on legacy browsers which predate CORS support.


In this blog, I will first explain how to use JSONP and then introduce the secret behind it.


JSONP in nodeJS server

Suppose I have two employee ID lookup service hosted by the port 3000 and 3001 in my local server. The service will simply return employee name by ID.


The client web page is hosted in port 3000. According to same origin policy, the web page hosted in port 3000 is allowed to access the service hosted in localhost:3000, but forbidden for localhost:3001.


image.png


Let’s now do a verification.

This is my server listening to port 3000:

image.png

And this is my client page which allows end user to type the employee ID and send the query request:

image.png

When I click Submit button, I get query response returned from service in port 3000 as expected:



image.png


And this is the log output in the console of service in port 3000:


image.png


Now I make small modification on the web page in port 3000, forcing it to send request to port 3001 instead:


image.png


And resend the id query, this time I saw the expected cross domain error message:

image.png



How to resolve cross domain issue using JSONP

Both minor changes in client and server side are necessary.


In service working in port 3001, I add a new service end point “request_jsonp”:

image.png

In client web page, I change the send AJAX data type from json to jsonp, and inform server that “please parse the callback function name from literal “callback” in request header.


image.png

Now send the query again from localhost:3000 page, and the request could successfully reach service in localhost:3001, handled there and return to localhost:3000 again:


image.png


Magic behind JSONP

In fact, no magic at all. The mechanism of JSONP just utilize the “benefit” that the HTML


When I send the AJAX call with data type jsonp, a new script element is created on the fly. The employee ID specified by end user is also appended as a query field in request header.


image.png


In server side, the response to this JSONP request is NOT json data, but a fragment of executable JavaScript code. I add a print statement to make it more clear:


image.png



Once this response returns to client side, it will get executed immediately there as a reaction to jsonp request sent from client.


For more detail explanation you can refer to Wikipedia How JSONP works.


JSONP in ABAP Server

Suppose I have a web page in system AG3/001 which would like to access service in AG3/815.

Create a ICF service in AG3/815:


image.png


AG3/001 has port 44354 and AG3/815 port: 44356

The web page is put in AG3/001 which has almost exactly the same code in previous nodeJS chapter.

This request fails as expected.


image.png


Now enhance the ICF handler class with JSONP support: return a string which contains executable JavaScript code instead:

image.png

And click on submit button of web page in AG3/001, this time it works:

image.png

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
如何使用 JavaScript 代码连接部署在 SAP ABAP 服务器上的 OData 服务试读版
如何使用 JavaScript 代码连接部署在 SAP ABAP 服务器上的 OData 服务试读版
28 0
Yii2支持哪些Web服务器?
Yii2支持哪些Web服务器?
24 0
vue2 axios跨域解决方法 和nodejs+express跨域
vue2 axios跨域解决方法 和nodejs+express跨域
103 0
nodejs各种框架跨域设置
nodejs各种框架跨域设置
111 0
打包ajax生成小工具|学习笔记
快速学习如何打包ajax生成小工具
24 0
express学习43-ajax的运行环境
express学习43-ajax的运行环境
30 0
【JavaWeb】一篇承载Ajax、Axios、Json的学习笔记~
概念:ajax(AJavascriptAndXML):异步的JavaScript和Xml。
56 0
Ajax学习参考文档
Ajax学习参考文档
202 0
基于Flask开发网站 -- 前端Ajax异步上传文件到后台
从网页界面(前端)上传文件到服务器(后端)
324 0
当遇到跨域开发时, 我们如何处理好前后端配置和请求库封装(koa/axios版)
我们知道很多大型项目都或多或少的采用跨域的模式开发, 以达到服务和资源的解耦和高效利用. 在大前端盛行的今天更为如此, 前端工程师可以通过nodejs或者Nginx轻松搭建起web服务器.这个时候我们只需要请求后端服务器的接口即可实现系统的业务功能开发.这个过程中会涉及到web页面向API服务器的跨域访问(由于受到浏览器的同源策略,但是业界已有很多解决方案,接下来会介绍).通过这种开发模式使得我们真正的实现了前后端完全分离.
228 0
在nodejs服务器和ABAP服务器上使用jsonp
在nodejs服务器和ABAP服务器上使用jsonp
57 0
使用JavaScript的FormData向SAP ABAP系统发起登录请求
使用JavaScript的FormData向SAP ABAP系统发起登录请求
74 0
在nodejs websocket回调函数中没法用this
在一个nodejs module顶层把数据保存在this的自定义属性里:
60 0
+关注
jerrywangsap
文章
问答
视频
来源圈子
更多
+ 订阅
相关课程
更多
相关电子书
更多
函数计算事件驱动的无服务器计算服务
立即下载
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载