错误:过帐请求期间禁止交叉原点为空

时间:2019-07-25 07:56:36

标签: javascript node.js jsdom

我正在使用Google表单作为注册表单,该表单链接到用作数据库的电子表格。在执行发布请求时,出现了跨原点null禁止错误。

我有一个HTML页面,其中包含js文件,名为postRegistrationInfoToDatabase.js,该文件包含对服务器端路由(称为signup.js)的发布请求。 在signup.js中,我正在使用对Google表单的发布请求以写入数据。我正在使用express.js在localhost:3000上运行整个过程

不幸的是,当我尝试多种方式或发送请求时,我感到沮丧:通过xhr,axios,在package.json中添加笑话“ testEnvironment”:“ node”

从register.html调用postRegistration ... js:

<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 其中deviceId和email等是html表单中的值。

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;
}

当前结果:

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

1 个答案:

答案 0 :(得分:0)

请参阅下面的链接

使用cors解决了各种问题。

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