문제

I am playing with Google API in javascript. I managed to get a list of my contact with the following code :

$.ajax({
    url: 'https://www.google.com/m8/feeds/contacts/default/full?access_token=' + access_token + '&alt=json',
    method: 'GET',

    error: function(error) {
        alert('An error has occured during contact creation.');
    },

    success: function(data, status){
        console.log(data);
    }
});

I tried to add a contact by changing GET to POST and adding my contact data in the request body. But as soon as I add a data attribute, or change GET to POST, the server answers me the really annoying "No 'Access-Control-Allow-Origin" error.

Any idea? I am following this documentation : https://developers.google.com/google-apps/contacts/v3/?csw=1#creating_contacts

Thanks a lot

도움이 되었습니까?

해결책

It is possible to do this from the browser, although not obvious at all.

Based on this SO answer, we learn that there is method called gapi.client.request that can be used for this (instead of jQuery's $.ajax).

Accordingly, for editing we can do:

gapi.client.request({
    method : 'PUT', 
    path:'m8/feeds/contacts/default/full/<contactId>/<editRevisionFromGET>', 
    body : {"version":"1.0","encoding":"UTF-8","entry": ...}, 
    callback : function(data) { 
        console.log(data); 
    }
});

The important parts for editing in here are:

  1. send back the entire entry you got before from a read
  2. use the current ID given at the end of the URL in the entry.link element with relation type edit (or you'll get a HTTP Status 409 - Conflict)

Side note: Notice that these requests actually are done to https://content.googleapis.com/ ... From some quick tests, it seems you can do ?all? requests just to that URL instead of google.com, and then CORS issues disappear.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top