我正在尝试复制此操作指南How-to guide中的示例,但我始终收到此错误:
XMLHttpRequest cannot load https://svcs.sandbox.paypal.com/AdaptiveAccounts/CreateAccount. Origin http://my.domain.com is not allowed by Access-Control-Allow-Origin.
可以为我提供一个工作示例或任何提示吗?谢谢!
这是我的代码:
Meteor.http.post("https://svcs.sandbox.paypal.com/AdaptiveAccounts/CreateAccount",{
headers: {
"X-PAYPAL-SANDBOX-EMAIL-ADDRESS": "myemail@domain.com",
"X-PAYPAL-SECURITY-USERID": "myuserid",
"X-PAYPAL-SECURITY-PASSWORD": "somepassword",
"X-PAYPAL-SECURITY-SIGNATURE": "thelongsignature",
"X-PAYPAL-APPLICATION-ID": "APP-80W284485P519543T",
"X-PAYPAL-DEVICE-IPADDRESS": "192.0.2.0",
"X-PAYPAL-REQUEST-DATA-FORMAT": "JSON",
"X-PAYPAL-RESPONSE-DATA-FORMAT": "JSON"
},
data: {
accountType: "PERSONAL",
name:{
firstName: "John",
lastName: "Doe"
},
address:{
line1: "123 Main Street",
city: "Sydney",
state: "NSW",
postalCode: "2000",
countryCode: "AU",
citizenshipCountryCode: "AU",
contactPhoneNumber: "12345678",
dateOfBirth: "1990-01-01Z",
createAccountWebOptions:{
returnUrl: "http://my.domain.com/"
},
currencyCode: "AUD",
emailAddress: "anotheremail@somedomain.com",
preferredLanguageCode: "en_AU",
registrationType: "Web",
requestEnvelope:{
errorLanguage: "en_US"
}
}
}
},
function(error,result){
alert(error);
alert(result.statuscode);
})
答案 0 :(得分:2)
你是在客户端这样做吗?从服务器端运行它会更安全,因为您的用户无法看到凭据,另外如果您从服务器请求文档,则不应该收到错误。您的网络浏览器阻止了该请求,因为它是cross origin request。
服务器
Meteor.methods({
'createaccount':function() {
var result = Meteor.http.post("https://svcs.sandbox.paypal.com/AdaptiveAccounts/CreateAccount", {
headers: {
"X-PAYPAL-SANDBOX-EMAIL-ADDRESS": "myemail@domain.com",
"X-PAYPAL-SECURITY-USERID": "myuserid",
"X-PAYPAL-SECURITY-PASSWORD": "somepassword",
"X-PAYPAL-SECURITY-SIGNATURE": "thelongsignature",
"X-PAYPAL-APPLICATION-ID": "APP-80W284485P519543T",
"X-PAYPAL-DEVICE-IPADDRESS": "192.0.2.0",
"X-PAYPAL-REQUEST-DATA-FORMAT": "JSON",
"X-PAYPAL-RESPONSE-DATA-FORMAT": "JSON"
},
data: {
accountType: "PERSONAL",
name:{
firstName: "John",
lastName: "Doe"
},
address:{
line1: "123 Main Street",
city: "Sydney",
state: "NSW",
postalCode: "2000",
countryCode: "AU",
citizenshipCountryCode: "AU",
contactPhoneNumber: "12345678",
dateOfBirth: "1990-01-01Z",
createAccountWebOptions:{
returnUrl: "http://my.domain.com/"
},
currencyCode: "AUD",
emailAddress: "anotheremail@somedomain.com",
preferredLanguageCode: "en_AU",
registrationType: "Web",
requestEnvelope:{
errorLanguage: "en_US"
}
}
}
});
return result;
});
客户端
Meteor.call('createaccount', function(error,result) { console.log(result); });
注意我已将您的请求更改为同步请求,以便它可以将数据返回到客户端