2013-10-06 20 views
0

我試圖讓從骨幹一個Ajax post請求,調用Laravel路線「買單」,但我總是得到從控制檯日誌中這樣的回答:訪問控制允許來源與Laravel和Omnipay

XMLHttpRequest cannot load https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&useraction=commit&token=EC-5S932878HU8059629. Origin http://localhost is not allowed by Access-Control-Allow-Origin. 

我試圖修改我的MAMP httpd.conf來接受跨域腳本,正如你所看到的,我已經爲routes.php添加了一個頭部指令。 這裏是我的JS代碼:

Backbone.ajax({ 
     url:'index.php/pay', 
     type:'POST', 
     dataType:"json", 
     data: converteditems, 
     crossDomain: true, 
     success:function (data) { 
      if(data.error) { // If there is an error, show the error messages 
       $('.alert-error').text(data.error.text).show(); 
      }   
     } 
    }); 

這是Laravel的routes.php文件:

<?php 
header('Access-Control-Allow-Origin: *'); 
Route::get('/', function() 
{ 
return View::make('home'); 
}); 

Route::resource('products','ProductsController'); 

Route::resource('login', 'AuthenticationController'); 

Route::post('pay','[email protected]'); 

的doPay方法使用Omnipay包以這種方式(用於測試目的OFC):

public function doPay() 

{   

$gateway = GatewayFactory::create('PayPal_Express'); 
$gateway->setUsername('blablablabla-facilitator_api1.gmail.com'); 
$gateway->setPassword('137787773'); 
$gateway->setSignature('AhFvPK5rU.kfQOKIwZcYO1yItmtHASGDFDFGDbY9.w'); 
$gateway->setTestMode('true'); 
$args['amount']='2.00'; 
$args['description']='Your purchase'; 
    $args['returnUrl']='http://localhost/shoppingcart/index.php/return'; 
    $args['cancelUrl']='http://localhost/shoppingcart/index.php/cancel'; 

try { 

$response = $gateway->purchase($args)->send(); 

if ($response->isSuccessful()) { 
$responsereturn=$response->getData();   
} elseif ($response->isRedirect()) {  
    $response->redirect(); 
} else { 
    exit($response->getMessage()); 
} 
} catch (\Exception $e) { 
exit('internal error, log exception and display a generic message to the customer'); 
} 
} 

從我的控制檯的頭部中的一些詳細信息:

Request URL:http://localhost/shoppingcart/public/index.php/pay 
Request Headersview source 
Accept:application/json, text/javascript, */*; q=0.01 
Content-Type:application/x-www-form-urlencoded; charset=UTF-8 
Origin:http://localhost 
Referer:http://localhost/shoppingcart/public/ 
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36 

Request URL:https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&useraction=commit&token=EC-6J290181UP558705C 
Request Headersview source 
Origin:http://localhost 
Referer:http://localhost/shoppingcart/public/ 
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36 
Query String Parametersview sourceview URL encoded 
cmd:_express-checkout 
useraction:commit 
token:EC-6J290181UP558705C 

回答

0

如果您想從Ajax發出請求,那麼您不希望omnipay立即將該請求重定向到paypal。您想要將客戶的瀏覽器重定向到paypal。

因此,您需要在控制器操作中添加一些邏輯,因此如果它是XHR請求,那麼只需返回URL即可轉發客戶。

E.g.使用$ response-> getRedirectUrl()而不是$ response-> redirect()。然後將其放入JSON響應中或使用JavaScript將客戶瀏覽器發送到該URL。

相關問題