我正在从 java 脚本函数对后端进行 JSONP 调用,以便在 json 中获取响应数据。如何从 java 结束?

I am doing a JSONP call to backend from javascript function in order to get the response data in json. How to do this from java end?

我正在使用 ajax 从 java 脚本触发以下跨域 JSONP 调用:

$.ajax({ 
  url: 'https://xyz.abc.com/xxx/xxx/client', 
  type: 'POST', 
  dataType: 'jsonp', 
  jsonpCallback: 'callback',
  data:{ id: '123456' },
  crossDomain: true,
  contentType: 'application/jsonp'
});

下面是从网络选项卡中看到的查询字符串参数:-

callback=callback&id=123456&_=1498907401152

但出于安全和防止漏洞的考虑,我想从 Java 端进行此调用。如何从 java 结束执行上面发布的等效 JSONP 调用?我正在使用 Struts2 操作 class。

Here 你阅读了:

JSONP is simply a hack to allow web apps to retrieve data across domains. It could be said that it violates the Same Origin Policy (SOP). The way it works is by using Javascript to insert a "script" element into your page.

所以当你想使用Java结束时,你不需要JSONP。只需在您的操作中执行 JSON HTTP POST(例如 here),处理响应并填写您的操作,然后 return 成功。