简体   繁体   English

没有JSONP的跨站点调用

[英]Cross site calls without JSONP

I am having a application where frontend being built using HTML, CSS and Javascript code. 我有一个使用HTML,CSS和Javascript代码构建前端的应用程序。 Backend will be created using core java, Restlet. 后端将使用核心Java Restlet创建。

Now the real problem is frontend and backend both will be on diff servers with diff ports too. 现在真正的问题是前端和后端都将在具有差异端口的差异服务器上。 like, frontend is on: http://clientLookup (just for example) And backend is on, http://lcgrke:8080 例如,前端位于: http://clientLookup (仅作为示例),后端位于, http://lcgrke:8080

Now as i will send the server or rest calls from frontend via Ajax Request or jQuery Ajax then i am getting the cross side scripting issue (SOP - same origin policy). 现在,由于我将通过Ajax Request或jQuery Ajax从前端发送服务器或其他电话,因此我遇到了跨脚本问题(SOP-相同来源策略)。 I am not getting how to get around this. 我没有解决这个问题。

JSONP can be one of the option, but it will work for only GET type calls only, but in my application i will have GET/POST requests. JSONP可以是选项之一,但它仅适用于GET类型的调用,但是在我的应用程序中,我将具有GET / POST请求。 Also, some urls of server wont be JSONP enabled (dont ask me why, just accept they will be non-editable), so JSONP doesnt seem to be better option. 另外,服务器的某些URL不会启用JSONP(不要问我为什么,只是接受它们将是不可编辑的),因此JSONP似乎不是更好的选择。

Can anyone please explain me how i will get around this issue? 谁能解释我如何解决这个问题?

I had the same issue not too long ago. 不久前我遇到了同样的问题。 You can install PHP on your frontend server and make the AJAX call to a PHP script on that server. 您可以在前端服务器上安装PHP,并在该服务器上对PHP脚本进行AJAX调用。 There are several HTTP libraries for PHP (cURL being the most popular) that you can then use to make an HTTP request to your backend server. 有几个用于PHP的HTTP库(cURL是最受欢迎的),您可以用来向后端服务器发出HTTP请求。 Basically you can write a PHP script on your frontend server to act as a middle man. 基本上,您可以在前端服务器上编写PHP脚本以充当中间人。

The modern way to handle cross site requests is using CORS instead of JSONP , although you have to be aware about which browsers support CORS. 处理跨站点请求的现代方法是使用CORS而不是JSONP ,尽管您必须知道哪些浏览器支持CORS。

You can use CORS with almost modern browsers (IE10, FF, Chrome, Safari, Opera), but not with IE9/8. 您可以在几乎所有现代浏览器(IE10,FF,Chrome,Safari,Opera)上使用CORS,但不能在IE9 / 8上使用。

With IE9/8 you can use another technique called XDomainRequest , but you must implement it via JSNI. 使用IE9 / 8,您可以使用另一种称为XDomainRequest的技术,但是必须通过JSNI实现。

The goal of using CORS vs JSONP is that in your server side you just add a filter and everything should work out-of-the-box (RPC, RF, etc). 使用CORS和JSONP的目标是,在服务器端只需添加一个过滤器,所有组件都可以立即使用(RPC,RF等)。

To use CORS in gwt, you can read this page in the gwtquery site where you have a filter example. 要在gwt中使用CORS,您可以在gwtquery站点中有过滤器示例的地方阅读此页面 In that page you also have useful info about jsonp, and how to use gwtquery ajax which simplifies the gwt RequestBuilder way. 在该页面中,您还将获得有关jsonp的有用信息,以及如何使用gwtquery ajax来简化gwt RequestBuilder方式。

If you are using PHP and have php_culr library available you might want to leverage the cross origin to the server. 如果您使用的是PHP并且具有php_culr库可用,则可能要利用交叉源到服务器。 You can see an example here: http://davidwalsh.name/curl-post or you could use file_get_contents function and serialize the posted parameters or just pass the get parameters you want (if needed). 您可以在此处看到一个示例: http : //davidwalsh.name/curl-post ,也可以使用file_get_contents函数并序列化所发布的参数,或者只是传递所需的get参数(如果需要)。

Hope this helps. 希望这可以帮助。

Ben Alman has a simple proxy script that I've used as a temporary workaround for this kind of situation. Ben Alman有一个简单的代理脚本,我将其用作这种情况的临时解决方法。

Basically, it forwards GET and POST requests using curl. 基本上,它使用curl转发GET和POST请求。

http://benalman.com/projects/php-simple-proxy/ http://benalman.com/projects/php-simple-proxy/

$url = $_GET['url'];
$ch = curl_init( $url );
curl_setopt($ch, CURLOPT_VERBOSE, true);
if ( strtolower($_SERVER['REQUEST_METHOD']) == 'post' ) {
  curl_setopt( $ch, CURLOPT_POST, true );
  //curl_setopt( $ch, CURLOPT_POSTFIELDS, $_POST );

  $vin = $_POST["vin"];
  $subscriberProgramXML = $_POST["subscriberProgramXML"];

  $data = array("vin" => $vin, "subscriberProgramXML" => $subscriberProgramXML);
  $data_string = json_encode($data);

  $httpHeader = array('Content-Type: application/json', 'Content-Length: ' .strlen($data_string));

  curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_string);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
}

curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

curl_setopt( $ch, CURLOPT_USERAGENT, $_GET['user_agent'] ? $_GET['user_agent'] : $_SERVER['HTTP_USER_AGENT'] );


list( $header, $contents ) = preg_split( '/([\r\n][\r\n])\\1/', curl_exec( $ch ), 2 );

$status = curl_getinfo( $ch );

curl_close( $ch );

// Set the JSON data object contents, decoding it from JSON if possible.
$decoded_json = json_decode( $contents );
$data['contents'] = $decoded_json ? $decoded_json : $contents;

// Generate appropriate content-type header.
$is_xhr = strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
header( 'Content-type: application/' . ( $is_xhr ? 'json' : 'x-javascript' ) );

// Get JSONP callback.
$jsonp_callback = $enable_jsonp && isset($_GET['callback']) ? $_GET['callback'] : null;

// Generate JSON/JSONP string
$json = json_encode( $data );

print $jsonp_callback ? "$jsonp_callback($json)" : $json;

that code is copypasted from the original php, but it's only part of the code. 该代码是从原始php复制粘贴的,但这只是代码的一部分。 It illustrates the solution. 它说明了解决方案。

As @Manolo said the way to go is using CORS (you can see more details here: http://blogs.mulesoft.org/cross-domain-rest-calls-using-cors/ - DISCLAIMER: I wrote that article, but to not make this answer a self promotion, you can search for CORS and you'll find similar articles). 正如@Manolo所说,要使用的方法是使用CORS(您可以在此处查看更多详细信息: http : //blogs.mulesoft.org/cross-domain-rest-calls-using-cors/-免责声明:我写了这篇文章,但是要使此答案不成为自我提升,您可以搜索CORS,然后会找到类似的文章)。

The only thing that I could add to Manolo response is that if you use jQuery you don't have to worry about IE's XDomainRequest, because jQuery takes account of those browser compatibility details. 我可以添加到Manolo响应中的唯一一件事是,如果您使用jQuery,则不必担心IE的XDomainRequest,因为jQuery考虑了这些浏览器兼容性的详细信息。

Also since you are using Restlet, this article will be helpful: http://kodemaniak.de/2010/07/cross-domain-ajax-with-restlet-and-jquery/ 另外,由于您正在使用Restlet,因此本文将对您有所帮助: http : //kodemaniak.de/2010/07/cross-domain-ajax-with-restlet-and-jquery/

I never worked with Restlet, but since is Java based, other simple option to add CORS is to create or use a Filter, here is one Apache license filter implementation: https://bitbucket.org/thetransactioncompany/cors-filter/src 我从未使用过Restlet,但是由于它是基于Java的,因此添加CORS的另一个简单选择是创建或使用过滤器,这是一个Apache许可证过滤器实现: https : //bitbucket.org/thetransactioncompany/cors-filter/src

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

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