1

我正在尝试复制此操作指南操作指南中的示例,我总是收到此错误:

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);
    })
4

1 回答 1

2

你在客户端这样做吗?从服务器端运行它会更安全,因为您的用户看不到凭据,此外,如果您从服务器请求文档,您也不应该收到错误消息。您的网络浏览器正在阻止该请求,因为它是一个跨源请求

服务器

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); });

注意我已将您的请求更改为同步请求,以便它可以将数据返回给客户端

于 2013-02-05T08:54:53.920 回答