0

I want to create new contact in google contact with google api with javascript and jquery, i can retrieve contacts with this sample :

jQuery.ajax({
    url: "https://www.google.com/m8/feeds/contacts/default/full?alt=json&" +
    "access_token=xs55.CjDOA8lRTs8567657567vXXXX&" +
    "max-results=100&" +
    "v=3.0",
    headers: {
        'Authorization': "Bearer xs55.CjDOA8lRTs8567657567vXXXX",
        'Content-Type': 'application/json'
    },
    method: "GET",
    dataType: 'jsonp',
    success: function (data) {
        console.log(data)
    },
    error: function (data) {
        console.log('error: ');
        console.log(data);
        console.log(data.status);
    }
})      

Now i want to POST data and create or update items in google document there is always error ! :(

For example in this code "Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://XXX' is therefore not allowed access. The response had HTTP status code 405." error happened :

jQuery.ajax({
    url: "https://www.google.com/m8/feeds/contacts/default/full?alt=json&" +
    "access_token=xs55.CjDOA8lRTs8567657567vXXXX&" +
    "max-results=100&" +
    "v=3.0",
    headers: {
        'Authorization': "Bearer xs55.CjDOA8lRTs8567657567vXXXX",
        'Content-Type': 'application/json'
    },
    method: "POST",
    data: {title: "vvvv", phonenumber: "3333"},
    //dataType: 'jsonp',
    success: function (data) {
        console.log(data)
    },
    error: function (data) {
        console.log('error: ');
        console.log(data);
        console.log(data.status);
    }
})  

whitout jsonp option there is no way to work that this option used when want to GET something not POST it .

alireza
  • 1,173
  • 4
  • 19
  • 40

1 Answers1

0

Can you try this:

data= {title: "vvvv", phonenumber: "3333"};
$.ajax({
    type: "POST",
    data :JSON.stringify(data),
    url: "https://www.google.com/m8/feeds/contacts/default/full?alt=json&" +
         "access_token=xs55.CjDOA8lRTs8567657567vXXXX&" +
         //"max-results=100&" +
         "v=3.0",
    contentType: "application/json"
});
Ankur Verma
  • 5,793
  • 12
  • 57
  • 93
FreedomPride
  • 1,098
  • 1
  • 7
  • 30
  • @alireza 405 is method not allowed.. what is the method they allowed.. if json , this should be the way else.. need to check their response protocol.. if it's GCM it's different. – FreedomPride Jan 11 '17 at 09:36
  • accord to this https://drive.google.com/file/d/0B392wMr_wQ1HcnZadnVpZWg4NUE/view?usp=sharing from google documentation POST method is allowed. – alireza Jan 11 '17 at 09:44
  • @alireza, i see you're sending it as JSON, example of JSON format has curly brackets and comma. The {email} is what you're basically trying to send ? yes? – FreedomPride Jan 11 '17 at 09:58
  • I just want to create contact in google contact and want to send some data ! in this situation i think data is not a main problem, but google api and how receiving POST request is main problem ! – alireza Jan 11 '17 at 11:13
  • @alireza, correct.. i'll figure out on how to try from my client too. – FreedomPride Jan 12 '17 at 04:26