0

I would like to get help with using ajax without jQuery to post data info a Google spreadsheet please.

As in jQuery it is really simple and goes like this:

var gformUrl = 'https://docs.google.com/forms/d/abcdefg1234567890/formResponse';

var gformData = {
    'entry.1405357202': 'text #1',
    'entry.2112718259': 'text #2',
    'entry.1657451952': 'text #3',
    'entry.737451165': 'text #4'
};

$.post(gformUrl,gformData);

Now even that it shows an error in the console for no cross origin, it still works great.

I'm trying to do not use jQuery and do that in pure javascript, but with no success just yet, here's my code:

var xhr = new XMLHttpRequest();
xhr.open('POST', gformUrl, true);
xhr.send(JSON.stringify(gformData));
2
  • You said you were getting a cross origin error, but the $.post() method still works by adding those entries in your spreadsheet? Commented Apr 22, 2015 at 23:10
  • YEP! the wonders of jQuery I guess ;) XMLHttpRequest cannot load https://docs.google.com/forms/d/abcdefg1234567890/formResponse. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. Commented Apr 22, 2015 at 23:11

2 Answers 2

1

$.post sends the data in URL-encoded format, not JSON. This is param=val&param=val&... format.

var formDataArray = [];
for (var key in gformData) {
    formDataArray.push(encodeURIComponent(key) + '=' + encodeURIComponent(gformData[key]));
}
xhr.send(formDataArray.join('&');
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but unfortunately it still doesn't works and just gives the cross origin error in the console as in jQuery but not working. XMLHttpRequest cannot load https://docs.google.com/forms/d/abcdefg1234567890/formResponse. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
As @boombox mentioned there is a need to set a request header, combine that with your code solved the issue, so thanks :)
0

If $.post() is working even with the Access-Control-Allow-Origin error, the culprit is likely to be the type of data that is being sent. According to the MDN article on CORS, a simple cross site request with POST requires the content-type of the data to be either application/x-www-form-urlencoded, multipart/form-data, or text/plain.

Currently, your XMLHttpRequest() sends JSON with a content-type of text/html. Let's fix that.

Knowing this, below is what you can try by setting the content-type request header to be application/x-www-form-urlencoded and sending encoded data.

--EDIT-- Changed my previous xhr.send(encodeURIComponent(JSON.stringify(gformData))); to Barmar's solution with my own twist to avoid the use of an array.

var gformUrl = 'https://docs.google.com/forms/d/abcdefg1234567890/formResponse';

var gformData = {
    'entry.1405357202': 'text #1',
    'entry.2112718259': 'text #2',
    'entry.1657451952': 'text #3',
    'entry.737451165': 'text #4'
};

var xhr = new XMLHttpRequest();
xhr.open('POST', gformUrl, true);
xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded; charset=UTF-8');

var formData = '';
for (var key in gformData) {
    formData += encodeURIComponent(key) + '=' + encodeURIComponent(gformData[key]) + '&';
}
xhr.send(formData.substr(0, formData.length-1));

1 Comment

So combine your answer (setting a request header) together with @Barmar code that convert the JSON info a URL-encoded format, did the job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.