Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

POST data to Google Spreadsheet, CORS

Hi, I'm trying to send form data from my website to a google docs spreadsheet. I've followed these instructions: https://wiki.base22.com/pages/viewpage.action?pageId=72942000

I'm receiving this error: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.

I've tried adding headers to my ajax call (Access-Control-Allow-Headers: *), and tried to use JSON-P, but neither of these have worked. Any help is much appreciated!! Here's my ajax call:

 $.ajax({
            type: "POST",
            crossDomain: true,
            url: url,
            data: {"entry.1415540910":first_name,
                 "entry.463264812": last_name,
                 "entry.645413332": email,
                 "entry.978444430":phone,
                 "entry.1368796298":comments
                },
            dataType: 'json',
            success: function() {
                  $('#contact_form').html("<div id='message'></div>");
                  $('#message').html("<h2>Contact Form Submitted!</h2>")
                   .append("<p>We will be in touch soon.</p>")
                   .hide()
                   .fadeIn(1500, function() {
                        $('#message').append("<img id='checkmark'  />");
                   });
             }
       });

please help!

1 Answer

Hello Angelina!

I must confess that I actually never did this. But, my 'guess' is that you should be getting the 'Access-Control-Allow-Origin' header from YOUR server response so that you can then perform an ajax request to another server/domain.

So you must set the header value on the server side.

So, since we're in the Javascript topic I will assume your (maybe) using express? (eheh)

If so, in a request handler function with the response object accessible, you should be able to set the header with something like:

function(req,res){
  res.header("Access-Control-Allow-Origin", "*");
}

Cheers! Pedro