2

I have already published a app script, and I test the query string in the browser, and it works well. I would like to send a xmlhttprequest to the script, but it shows =>

XMLHttpRequest cannot load https://script.google.com/macros /s/XXXXXXXXXX/exec. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://docs.google.com' is therefore not allowed access.

This is the app script code:

function doGet(e){
  Logger.log(e.parameter.id);
  //other function
  return ContentService.createTextOutput("Hello World");
}

Here is the client code:

$.ajax({
    url:'https://script.google.com/macros/s/XXXXXXXX/exec',
    method:'POST',
    data:{
        id: "123123"
    },
    success:function(){
        console.log("success");
    }
});
Oberon
  • 628
  • 1
  • 6
  • 12
  • 1
    Change `doGet(e)` to `doPost(e)` and see what happens. – Alan Wells Mar 17 '16 at 01:36
  • 1
    I rarely see the `Logger` work when the URL is called in this scenario so you may want to consider another route of inspecting the parameter like posting to a spreadsheet or script properties – Bryan P Mar 17 '16 at 02:57
  • @SandyGood I have changed to doPost(e) already, but it still doesn't work. – Oberon Mar 17 '16 at 05:26
  • 1
    Look at this https://developers.google.com/apps-script/guides/content#serving_jsonp_in_web_pages – tagplus5 Mar 17 '16 at 11:53
  • What are the permission settings in the Apps Script project? If it's not open for anyone to run, then you'd need to authenticate with AOuth2. – Alan Wells Mar 17 '16 at 14:49

1 Answers1

0

There are a few possibilites, and I have ran into this error before myself. As I have insufficient reputation to comment and ask for clarification, I will write the most probable cause.

This problem has 2 parts - You can't post (unstringified) objects, and errors on google apps script do not have CORS headers

solution: stringify and parse the object

Without converting the object to a string, your browser will just send [Object object] and not the information. This will cause an error on your script, and error messages by Google Script are HTML webpages that don't have CORS headers, which triggers the CORS error that does not really tell you the true problem

To be able to successfully POST the parameters, you have to convert the object to a string (e.g. by using JSON.stringify()), get the value through e.postData.contents on your google apps script, parse it, before you can use it as if it were an object.

I personally found when I read that I can't send raw objects in javascript - pass object via post

You mentioned that the code worked when you tested it using the query string in your browser, however, it is different as that will be a GET request and will trigger doGet instead of doPost

this is the most probable explanation and hope it helps!

Do test your code with default values before deploying it as once you deploy your code it can get very troublesome. You can use https://hurl.it to see what actually comes out without cors errors in the way.

Ansel Lee
  • 43
  • 1
  • 7