简体   繁体   English

我正在从javascript函数对后端进行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?

I am triggering the below crossdomain JSONP call from javascript using ajax: 我正在使用ajax从javascript触发以下跨域JSONP调用:

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

Below are the Query string parameters as seen from the network tab:- 以下是从网络标签中看到的查询字符串参数:

callback=callback&id=123456&_=1498907401152

But due to security and to prevent vulnerability I want to do this call from Java end. 但是由于安全性和防止漏洞的原因,我想从Java端进行此调用。 How can I do an equivalent JSONP call as posted above from java end? 我如何从Java端进行上述发布的等效JSONP调用? I am using a Struts2 action class. 我正在使用Struts2动作类。

At Here you read: 这里,您阅读:

JSONP is simply a hack to allow web apps to retrieve data across domains. JSONP只是一种黑客,它允许Web应用程序跨域检索数据。 It could be said that it violates the Same Origin Policy (SOP). 可以说它违反了“同一个起源策略”(SOP)。 The way it works is by using Javascript to insert a "script" element into your page. 它的工作方式是使用Javascript在页面中插入一个“脚本”元素。

So when you want to use Java end, you don't need JSONP. 因此,当您想使用Java end时,不需要JSONP。 Just do a JSON HTTP POST (like here ) in your action, process response and fill your action and return SUCCESS. 只需在您的操作中执行JSON HTTP POST(如此 ),处理响应并填写您的操作并返回SUCCESS即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM