1

I am using a google form that serves as a signup form which is linked to a spreadsheet used as database. While performing a post request, I am getting the cross origin null forbidden error.

I have an Html page with js file called postRegistrationInfoToDatabase.js which holds a post request to a route on the server-side called signup.js in signup.js I am using the post request to the google form in order to write the data. I am running the entire process on localhost:3000 using express.js

unfortunately I am getting frustrated as I tried numerous ways or sending the request: via xhr, axios, adding jest "testEnvironment": "node" in package.json

calling postRegistration...js from register.html:

<form id="form" target="_self" onsubmit="postToGoogle()" action="" autocomplete="on">
        <input id="bID" name="i_id" placeholder="Given ID" type="text" required>
        <input id="emailField" name="i_email" placeholder="Email" type="email" required>
        <input id="passwordField" name="i_password" placeholder="Password" type="password" required>
        <input id="phoneField" name="i_phone" type="telephone" placeholder="Mobile +44506789872"
          pattern="[+]{1}[0-9]{11,14}" required>
        <button id="send" type="submit" class="common_btn">Create Account</button>
      </form>

postRegistrationInfoToDatabase.js where deviceId and email etc.. are values from of the html form.

function postToGoogle() { ...    
    $.ajax({
        url: "/signup",
        data: { "deviceId": deviceId, "email": email, "password": password, "phone": phone },
        type: "POST",
        success: function (d) {
        },
        error: function (x, y, z) {

            $('#success-msg').show();
            $('#form').hide();
}

signup.js

{
var express = require('express');
var router = express.Router();

var jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window } = new JSDOM();
const { document } = (new JSDOM('')).window;
global.document = document;

var $ = jQuery = require('jquery')(window);
//const axios = require('axios');

/* GET sign up page. */
router.post('/', function (req, res, next) {

$.post('https://docs.google.com/forms/d/e/*formId*/viewform', {
    "entry.10***": req.body.deviceId, "entry.10***": req.body.email, 
    "entry.83***": req.body.password, "entry.11***": req.body.phone },
    function (data) {
        res.send("applied");
    });
});

     module.exports = router;
}

Current outcome:

Error: Cross origin null forbidden
at dispatchError (d:\nodejs-demo\node_modules\jsdom\lib\jsdom\living\xhr-utils.js:54:19)
at Object.validCORSHeaders (d:\nodejs-demo\node_modules\jsdom\lib\jsdom\living\xhr-utils.js:66:5)
at receiveResponse (d:\nodejs-demo\node_modules\jsdom\lib\jsdom\living\xmlhttprequest.js:843:21)
at Request.client.on.res (d:\nodejs-demo\node_modules\jsdom\lib\jsdom\living\xmlhttprequest.js:678:38)
at Request.emit (events.js:189:13)
at Request.onRequestResponse (d:\nodejs-demo\node_modules\request\request.js:1066:10)
at ClientRequest.emit (events.js:194:15)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:556:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:109:17)
at TLSSocket.socketOnData (_http_client.js:442:20) undefined
Tom.A
  • 436
  • 4
  • 9

1 Answers1

-2

Refer below link

there are various problem solved with cors.

Origin null is not allowed by Access-Control-Allow-Origin

raj m
  • 1,863
  • 6
  • 21
  • 37
  • Hi @raj , unfortunately, none of the suggestions worked for me – Tom.A Jul 25 '19 at 08:42
  • Did you try putting data type in payload ? but yes, If you run with "--allow-file-access-from-files" in chrome it should work – raj m Jul 25 '19 at 08:43
  • And make sure what server is expecting. Try to change from server also – raj m Jul 25 '19 at 08:45
  • you can message personally if you need more info – raj m Jul 25 '19 at 08:45
  • the --allow-file-access-from-files doesn't seem to work either – Tom.A Jul 25 '19 at 09:21
  • you have close all chrome browser and add --allow-file-access-from-files, then you need to open chrome – raj m Jul 25 '19 at 09:22
  • if you are using windows try this command "chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security " or in mac "open /Applications/Google\ Chrome.app --args --user-data-dir="/var/tmp/Chrome dev session" --disable-web-security", make sure you have closed all browser window – raj m Jul 25 '19 at 09:44