当前位置: 首页 > 知识库问答 >
问题:

节点。js路由无法识别正确的JSON

宣熙云
2023-03-14

相对新的节点。js,尝试从Angular2获取一个js对象到我的节点。js路由,但我总是从本机对象解析器获得一个标准的“索引0处的意外标记”。

对象很好地到达我的节点工厂,但在发布到路由时失败。

这是被发送到路由的JSON:

{"SQLServerHostName":"MININT-MT0DKDT","SQLServerDBName":"ContosoRetailDW","SQLServerUserName":"svc_sql_D3","SQLServerPassword":"Inspur123#@!","StoredProcedureName":"[dbo].[d3Test]"}

这是我在开发人员控制台中收到的错误。

angular2.dev.js:23083异常:语法错误:位置0BrowserDomA的JSON中的意外令牌Sdapter.log错误@angular2.dev.js:23083BrowserDomAdapter.log组@angular2.dev.js:23094ExceptionHandler.call@angular2.dev.js:1185(匿名函数)@angular2.dev.js:12591NgZone._notifyOnError@angular2.dev.js:13635onError@angular2.dev.js:13539Zone.run@angular2-polyfills.js:1247(匿名函数)@angular2.dev.js:13558zoneBoundFn@angular2-polyfills.js:1220angular2.dev.js:23083STACKTRACE: BrowserDomAdapter.logError@angular2.dev.js:23083ExceptionHandler.call@angular2.dev.js:1187(匿名函数)@angular2.dev.js:12591NgZone._notifyOnError@angular2. dev. js: 13635onError@angular2. dev. js: 13539Zone. run@angular2-polfills. js: 1247(匿名函数)@angular2. dev. js: 13558zoneBoundFn@angular2-polfills. js: 1220 angular2. dev. js: 23083同步错误:在XMLHttpask. req. onload[作为_onload](超文本传输协议://localhost: 1557/app/service/xhr-factory. service. js: 17:33)在Zone. run(超文本传输协议://localhost: 1557/node_modules/angular2/bundles/angular2-polyfills.js: 1243:24)在Zone. run(超文本传输协议://localhost: 1557/node_modules/angular2/bundles/angular2. dev. js: 13558:32)

-----异步间隙------在_GetStackTraceWithUnCughtError处出错(http://localhost:1557/node_modules/angular2/bundles/angular2-多填料。js:2244:29)在分区。叉子(http://localhost:1557/node_modules/angular2/bundles/angular2-多填料。js:2293:47)在区域。束缚(http://localhost:1557/node_modules/angular2/bundles/angular2-多填料。js:1218:53)在XMLHttpRequest。obj。加法器(http://localhost:1557/node_modules/angular2/bundles/angular2-多填料。js:1503:95)在XMLHttpRequest。描述设置[作为加载](http://localhost:1557/node_modules/angular2/bundles/angular2-多填料。js:1449:19)在评估时(http://localhost:1557/app/services/xhr-工厂。服务js:11:24)在lib$es6$promise$$internal$$initializePromise(http://localhost:1557/node_modules/angular2/bundles/angular2-多填料。js:515:10)在新的lib$es6$promise$promise$$promise(http://localhost:1557/node_modules/angular2/bundles/angular2-多填料。js:806:10)at_sendRequest(http://localhost:1557/app/services/xhr-工厂。服务js:7:16)在对象上。职位(http://localhost:1557/app/services/xhr-工厂。服务js:39:28)

异步间隙在_getStacktraceWithUncaughtError(http://localhost:1557/node_modules/angular2/bundles/angular2-polyfills.js: 2244:29)在Zone.fork(http://localhost:1557/node_modules/angular2/bundles/angular2-polyfills.js: 2293:47)在参数(匿名函数)(http://localhost:1557/node_modules/angular2/bundles/angular2-polyfills.js: 1671:82)

-----异步间隙------在_GetStackTraceWithUnCughtError处出错(http://localhost:1557/node_modules/angular2/bundles/angular2-多填料。js:2244:29)在分区。叉子(http://localhost:1557/node_modules/angular2/bundles/angular2-多填料。js:2293:47)在NgZone_createInnerZone(http://localhost:1557/node_modules/angular2/bundles/angular2.dev.js:13546:39)在新工业区(http://localhost:1557/node_modules/angular2/bundles/angular2.dev.js:13412:32)在createNgZone(http://localhost:1557/node_modules/angular2/bundles/angular2.dev.js:12475:12)在平台上。应用(http://localhost:1557/node_modules/angular2/bundles/angular2.dev.js:12550:31)反对。引导(http://localhost:1557/node_modules/angular2/bundles/angular2.dev.js:24805:64)执行时(http://localhost:1557/app/boot.js:14:23)至少,重新评估(http://localhost:1557/node_modules/systemjs/dist/system.src.js:3186:26)反对。执行(http://localhost:1557/node_modules/systemjs/dist/system.src.js:3304:13)浏览器适配器。logError@angular2。dev.js:23083异常处理程序。打电话给@angular2。dev.js:1188(匿名函数)@angular2。dev.js:12591NgZone_notifyOnError@angular2。dev.js:13635onError@angular2。dev.js:13539Zone。运行@angular2 polyfills。js:1247(匿名函数)@angular2。dev.js:13558zoneBoundFn@angular2 polyfills。js:1220 angular2。dev.js:1206未捕获语法错误:JSON中位置0req处出现意外的令牌S。在xhr工厂进行加载。服务ts:28分。运行@angular2 polyfills。js:1243(匿名函数)@angular2。dev.js:13558zoneBoundFn@angular2 polyfills。js:1220

这是Angular2组件,它调用工厂

import {Component} from 'angular2/core';
import {WizardStepOneComponent} from './wizardStepOne.component'
import {WizardStepTwoComponent} from './wizardStepTwo.component'
import {WizardStepThreeComponent} from './wizardStepThree.component'
import {ConfigurationData} from '../classes/configurationData'
import {Report} from '../classes/report'
import {ChartType} from '../classes/chartType'
import {WizardFactory} from '../services/WizardFactory.service'

@Component({
    selector: 'wizardMain',
    templateUrl: '../../partials/_wizardMain.html',
    directives: [WizardStepOneComponent
               , WizardStepTwoComponent
               , WizardStepThreeComponent],
    providers: [Report, ChartType]
})

export class WizardMainComponent{
        configurationData: ConfigurationData;
        report: Report;
        chartType: ChartType;
        step1Show:boolean;
        step2Show:boolean;
        step3Show:boolean;
        step4Show:boolean;

        configMainShow:boolean;
        configSQLServerShow:boolean;
        configMySQLShow:boolean;
        configHadoopShow:boolean;
        configNoSQLShow:boolean;

    constructor(public _report: Report
               ,public _chartType: ChartType){
        this.report = _report;
        this.chartType = _chartType;
        this.hideAll();
        this.loadStep(1);
    }

    hideAll(){
        this.step1Show = false;
        this.step2Show = false;
        this.step3Show = false;
        this.step4Show = false;

        this.configMainShow = false;
        this.configSQLServerShow = false;
        this.configMySQLShow = false;
        this.configHadoopShow = false;
        this.configNoSQLShow = false;
    }

    loadAdminPanel(){

    }

    loadMainPanel(){

    }

    loadStep(stepNumber){
        switch(stepNumber)
        {
            case 1:
            {
                this.step1Show = true;
                this.step2Show = false;
                this.step3Show = false;
                this.step4Show = false;

                this.configMainShow = false;
                this.configSQLServerShow = false;
                this.configMySQLShow = false;
                this.configHadoopShow = false;
                this.configNoSQLShow = false;
                break;
            }
            case 2:
            {   
                this.step1Show = false;
                this.step2Show = true;
                this.step3Show = false;
                this.step4Show = false;

                this.configMainShow = false;
                this.configSQLServerShow = false;
                this.configMySQLShow = false;
                this.configHadoopShow = false;
                this.configNoSQLShow = false;

                break;
            }
            case 3:
            {
                this.step1Show = false;
                this.step2Show = false;
                this.step3Show = true;
                this.step4Show = false;

                this.configMainShow = false;
                this.configSQLServerShow = false;
                this.configMySQLShow = false;
                this.configHadoopShow = false;
                this.configNoSQLShow = false;
                break;
            }
            case 4:
            {
                this.step1Show = false;
                this.step2Show = false;
                this.step3Show = false;
                this.step4Show = true;

                this.configMainShow = false;
                this.configSQLServerShow = false;
                this.configMySQLShow = false;
                this.configHadoopShow = false;
                this.configNoSQLShow = false;
                break;
            }
        }
    }

    setConfigurationData(ConfigData: ConfigurationData){
        this.configurationData = ConfigData;
    }

    setReportName(ReportName: string){
        this.report.ReportName = ReportName;
        this.loadStep(2);
    }

    setChartType(ChartTypeID: number){
        this.chartType.ChartTypeID = ChartTypeID;
        WizardFactory.getColumnProperties(this.configurationData).then((data) => {
            console.log("SQL Server Response: " + data);
        });
        this.loadStep(3);
    }

    setXAxis(XAxis: string){
        this.configurationData.XAxis = XAxis;        
    }

    setYAxis(YAxis: string){
        this.configurationData.YAxis = YAxis;
        this.loadStep(4);
    }
}

在这里,它可以很好地到达工厂:

import {$http} from './xhr-factory.service';
import {ConfigurationData} from '../classes/configurationData';

export const WizardFactory = {
    getColumnProperties: function (currentConfig) {
        console.log("Current Config in Factory: " + JSON.stringify(currentConfig));
        return $http.post('/api/getColumnProperties', currentConfig);
    }
}

和我的XHR处理程序:

    export const $http = {
    get: function(url: string) {
        return _sendRequest(url, null, 'GET');
    },
    post: function(url: string, payload: any){
        return _sendRequest(url, payload, 'POST');
    },
    put: function(url: string, payload: any){
        return _sendRequest(url, payload, 'PUT');
    },
    delete: function(url: string, payload: any){
        return _sendRequest(url, null, 'DELETE');
    }
}


function _sendRequest(url: string, payLoad: any, type: string): Promise<JSON> {
        return new Promise(function(resolve, reject) {
            var req = new XMLHttpRequest();
            req.open(type, url);
            req.setRequestHeader("Content-Type", "application/json;charset=UTF-8");

            req.onload = function() {
                if (req.status == 200) {
                    console.log("Resolved");
                    resolve(JSON.parse(req.response));
                } else {
                    reject(JSON.parse(req.response));
                }
            };

            req.onerror = function() {
                reject(JSON.parse(req.response));
            };

            if (payLoad) {
                req.send(JSON.stringify(payLoad));
            } else {
                req.send(null);
            }
        });
    }

但是它不会到达我定义的路由;我已经操作了大约100次来尝试正确的语法:

var express = require('express');
var wizardRouter = express.Router();
var sql = require('mssql');

var sqlReturnDefinitionQuery = "SET NO_BROWSETABLE ON; \
                                SET FMTONLY ON; \
                                EXEC {0}; \
                                SET FMTONLY OFF; \
                                SET NO_BROWSETABLE OFF";
/*Get Stored Procedure Schema*/
wizardRouter.post('/getColumnProperties', function (req, res, next) {
    console.log("In router");
    _currentConfig =  JSON.parse(req.params.currentConfig);

    console.log("Current Config in Router: " + req.params.currentConfig);

    var config = {
        user: _currentConfig.SQLServerUserName,
        password: _currentConfig.SQLServerPassword,
        server: _currentConfig.SQLServerHostName,
        database: _currentConfig.SQLServerDBName
    };

    var finalReturnDefinitionQuery = sqlReturnDefinitionQuery.replace("{0}", _currentConfig.StoredProcedureName);

    sql.connect(config, function () {
        if(err) console.log(err);

        var request = new sql.Request();

        request.query(finalReturnDefinitionQuery, function(err, recordset){
            if(err) console.log(err);

            res.send(recordset);
        });
    });
});
module.exports = wizardRouter;

最后是我的节点服务器文件:

var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var wizard = require('./routes/wizard');

var app = express();

// view engine setup
app.set('/', path.join(__dirname, 'partials'));
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);

app.use('/js', express.static(__dirname + '/js'));
app.use('/app', express.static(__dirname + '/app'));
app.use('/content', express.static(__dirname + '/content'));
app.use('/node_modules', express.static(path.join(__dirname, '/node_modules')));
app.use('/partials', express.static(path.join(__dirname, '/partials')));


app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/api/', wizard);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

var server = app.listen(1557, function () {
    var host = 'localhost';
    var port = server.address().port;
    console.log('App listening at http://%s:%s', host, port);
});

module.exports = app;

任何帮助都将不胜感激;这快把我逼疯了。

共有1个答案

法浩壤
2023-03-14

所以,我认为它在你的req.onload函数上失败了,因为从你的服务器返回的数据是无效的JSON。抛出错误的是JSON. parse(req.response)。

我注意到,在您的服务器代码中,您没有将字符串化的数据发送回Angular应用程序。尝试设置res.send(记录集);res.send(JSON. stringify(记录集));

 类似资料:
  • 我有一个Kubernetes集群。这些节点位于具有IPs的内部网中,pod网络范围为。 问题是,一些工作节点在其他节点上有无法到达的pod网络路由,例如: 我尝试重新启动一个中断的节点,将其从集群中删除,运行,然后重新加入它。但一切都保持不变。 可能的原因是什么?我应该如何解决这个问题?提前多谢。

  • 我正在尝试使用 curl 在 pinterest.com 上执行登录。我得到了以下请求-响应-流: GET请求登录表单并清除隐藏字段(csrftoken) POST请求登录凭据(邮件和密码)和scraped csrftoken 接收会话Cookie以进行登录 使用Curl,我可以看到以下发送和接收的标头: 因此,在第1步之后,设置了两个cookie csrfToken和_pinterest_ses

  • 问题内容: Node JS和NPM之前运行良好。最近,我重新安装了Node JS,NPM,问题开始了。在我安装了例如example 的模块后,该模块安装成功,但给出 ‘bower’不被识别为内部或外部命令,可操作程序或批处理文件。 我已经检查了所有旧模块的安装路径 。我试图卸载它们并重新安装模块,但是仍然出现相同的错误。 甚至我都删除了整个文件夹并再次安装了所有模块,但是结果是一样的。 我不知道为

  • 生成PDF框 正如您所看到的,左侧的签名面板是无效的,但左侧的签名字段是存在的并且可以工作。 我用PDFTRON生成相同的PDF。这就是结果: 生成的PDF Tron 在这种情况下,左侧的签名面板正确显示签名字段的存在。 我想获得这第二个案例(正确),但我不明白为什么PDF框可以这样做。 多谢

  • 所以我对Node和Webpack还不熟悉,我很难让我的项目正确编译。每次我将其加载到浏览器时,都会出现错误:。这是我的一份网页。配置。js文件: 这是我的主要作品的副本。js文件: 最后,这里是我已安装的节点包的列表: Babel-core Babel-loader Babel-preet-es2015 巴贝尔-预设-反应 巴贝尔-预设-阶段-0 Babelify 反应 report-dom we