Google App Script jQuery ajax request failed

问题内容:

I create a Google App Script to save form data in a spreadsheet. But I have some issues with communication between my jQuery script and Google App Script (MIME, CORS etc…)

It’s my Google App script:

function doGet(e) {
  var ss = SpreadsheetApp.openById("xxxxxxxxxxxxx");

  var SheetResponses = ss.getSheetByName("Responses");

  // Create a 'Responses' sheet if it does not exist.
  if(!SheetResponses){
    SheetResponses = ss.insertSheet("Responses");
  }

  SheetResponses.appendRow([new Date(),e.parameter.firstname, e.parameter.lastname, e.parameter.email, e.parameter.company]);
  return ContentService.createTextOutput(e.parameter).setMimeType(ContentService.MimeType.JAVASCRIPT);
}

And my jQuery request:

$.ajax({
        method: "GET",
        contentType: "application/javascript",
        crossDomain: true,
        dataType: "jsonp",
        url: "https://script.google.com/macros/s/xxxxxxxxx",
        data: { lastname: lastname, firstname: firstname, email: email, company: company }
        })
.done(function(received) {
        console.log(received);
})
.fail(function(received) {
        console.log('fail ',received);
});

The issue returned is it:

Uncaught SyntaxError: Unexpected identifier
{“firstname”:””,”callback”:”jQuery311014997333403236945_1512992290405″,”company”:””,”email”:””,”lastname”:””,”_”:”1512992290406″}

Thanks.

问题评论:

    
also Consider changing .done to success:function(response) to make sure that the ajax call was successful.
    
@AliSheikhpour Thanks, you are right, but with my issue, it’s the fail method called.
– tibeoh
4 hours ago
    
Are you sure the url is script.google.com or sites.google.com ?
    
@AliSheikhpour Yes I’m sure, I did Publish menu > Deploy as a web app… And URL is OK.
– tibeoh
4 hours ago

原文地址:

https://stackoverflow.com/questions/47752139/google-app-script-jquery-ajax-request-failed

添加评论

友情链接:蝴蝶教程