2

Google Contacts API バージョン 3.0 を使用しており、連絡先リストからリクエスト データのアドレスの 1 つを呼び出すと、次のエラーが発生しました。

XMLHttpRequest cannot load https://www.google.com/m8/feeds/contacts/XXXX@gmail.com/full. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://YYYY.ir' is therefore not allowed access. The response had HTTP status code 405.

そして、これは私のコードです:

function PopupFunc() {
jQuery("input[title*='GetgoogleButt1']")
    .off()
    .on('click', function (event) {
        console.log('clicked');
        var lnk = "https://accounts.google.com/o/oauth2/v2/auth?" +
            //"scope=email profile&" +
            "scope=https://www.googleapis.com/auth/contacts.readonly&" +
            //"scope=https://www.googleapis.com/auth/user.phonenumbers.read&" +
            "state=/profile&" +
            "redirect_uri=http://XXXX/Mypagerdi.html&" +
            "response_type=token&" +
            "client_id=MUCLIENTID.apps.googleusercontent.com&" +
            "output=embed";

        var authTab = window.open(lnk, "Auth", "_blank, width=600, height=600");

        jQuery(window).off().on("message", function (e) {
            if ( e.originalEvent.origin == "http://XXXX/" ) {
                console.log("message is : ", e.originalEvent.data)
                var hashdic = e.originalEvent.data;
                var authrz = hashdic["token_type"] + ' ' + hashdic["access_token"];
                console.log(hashdic["expires_in"])
                console.log(authrz)
                if ( hashdic["#error"] == undefined ) {
                    jQuery.ajax({
                        url: "https://www.google.com/m8/feeds/contacts/YYYY@gmail.com/full",
                        headers: {
                            'Authorization': authrz,
                            'Content-Type': 'application/json'
                        },
                        method: 'GET',
                        success: function (data) {
                            console.log('succes: ' + data);
                        }
                    });
                }
            }
        });
    });
}
PopupFunc();

認証情報に Authorized JavaScript origins を追加しましたが、このエラーはまだ存在します!

編集

これは ajax 本体の欠落部分ですdataType: 'jsonp' @Mehmet に感謝

4

1 に答える 1