我试图从主干发出Ajax帖子请求,称Laravel路由为"pay“,但我总是从控制台日志中得到这样的答复:
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();
}
}
});这是拉勒维尔的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','PaypalController@doPay');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发布于 2013-10-06 21:47:43
如果您想从Ajax中提出请求,那么您不希望杂费立即将该请求重定向到paypal。您希望将客户的浏览器重定向到paypal。
因此,您需要在控制器操作中添加一些逻辑,所以如果它是XHR请求,那么只需返回URL将客户转发到。
例如,在总括响应上使用$ response ->getRedirectUrl(),而不是使用$response->redirect()。然后将其放入JSON响应或其他内容中,并使用JavaScript将客户浏览器发送到该URL。
https://stackoverflow.com/questions/19208056
复制相似问题