GVKun编程网logo

JSON中位置4上的意外令牌<

19

最近很多小伙伴都在问JSON中位置4上的意外令牌这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展Angular2SyntaxError:JSON.parse()中位置0的JSON中

最近很多小伙伴都在问JSON中位置4上的意外令牌<这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展Angular 2 SyntaxError:JSON.parse()中位置0的JSON中的意外标记<、angularjs – 语法错误:令牌’:’是将变量传递给指令时的意外令牌、Browserify,Babel 6,Gulp-点差运算符上的意外令牌、JSON + Node.js-意外令牌o等相关知识,下面开始了哦!

本文目录一览:

JSON中位置4上的意外令牌<

JSON中位置4上的意外令牌<

var url="http://fsa.citop.in/lnct/service/signProcess.aspx";var data={txtLogId: "abc@xyz.com",txtLogPass: "xyz",hdnReqType2: "sign87162"};var success=function(data, textStatus, jqXHR) {            console.log(data);         };var fail=function(jqXHR, textStatus, errorThrown) {         console.log("Error:" + errorThrown );     }$.ajax({  type: "POST",  url: url,  data:data,  success:success,  error:fail,});

在chrome SyntaxError: Unexpected token < in JSON at position 4页面“
http://fsa.citop.in/lnct/ ” 的控制台中,该POST请求给我错误。

但是,如果我使用fsa.citop.in/lnct/service/signProcess.aspx(即不使用http://),它不会给我带来任何错误,但数据不会返回任何内容。在successPOST请求中,需要一个JSON对象。请有人解释这里发生了什么以及如何解决。

答案1

小编典典

这很可能是因为响应是HTML,并且正在尝试将其解析为其他内容。该<4位是第一<的<!DOCTYPE html...

您应该尝试在ajax调用中指定dataType(请参阅http://api.jquery.com/jquery.ajax/),并使其signProcess.aspx返回更有用的内容(当前响应内容类型为,application/json但它会打印HTML)。

Angular 2 SyntaxError:JSON.parse()中位置0的JSON中的意外标记<

Angular 2 SyntaxError:JSON.parse()中位置0的JSON中的意外标记<

我从Visual Studio中的Angular2组件服务调用Web API,但我不断收到错误“ JSON.parse()中位置0的JSON中的意外标记<”. ComponentService:

import { Injectable } from '@angular/core';
       import {Http,Response } from '@angular/http';
       import { IData } from '../Common/details';
       import { Observable } from 'rxjs/Observable';
       import 'rxjs/add/operator/map';

       @Injectable()  
       export class AniversaryService {
         constructor(private _http:Http) { }
         getimages(): Observable<IData[]> {
                return this._http.get("/api/ImageService/Details")
                .map((response: Response) => <IData[]>response.json()      
                };
        }

和相应的组件是:

import { Component,OnInit } from '@angular/core';
    import { DomSanitizer } from '@angular/platform-browser';
    import { IData } from '../Common/details';
    import { AniversaryService } from './Aniversary.service';

    @Component({
    selector: 'my-AniversaryComponent',providers: [AniversaryService]
    })

    export class AniversaryComponent implements OnInit {
       data: IData[];
       constructor(private _aniversaryservice: AniversaryService) { }
       ngOnInit() {
       this._aniversaryservice.getimages().subscribe((details) => this.data 
       =details); 
       }
     }

    }

这些是网络标头和我的响应的图像(响应是在Javascript中):

In Developer tool the network Headers image:

and my response is in Javascript

有时我的状态代码显示200 ok和内容类型(在响应标题中):application / javascript

请帮我解决这个问题.

我在这里先向您的帮助表示感谢

解决方法

标题没用,数据也是如此.但错误信息很明确:您认为应该是JSON,以“<”开头作为第一个字符,JSON不会以“<”开头.很可能你收到的是html或xml.

angularjs – 语法错误:令牌’:’是将变量传递给指令时的意外令牌

angularjs – 语法错误:令牌’:’是将变量传递给指令时的意外令牌

我有一个指令iframely,我在这样一个ng重复,像这样:
<iframely url="iterator.url"></iframely>

这只是将该值视为字符串“iterator.url”,而不是实际的.url值.要实验,我只是直接放入一个URL:

<iframely url="https://soundcloud.com/braxe1/braxe-one-more-chance"></iframely>

哪个给我语法错误:令牌’:’是一个意外的令牌错误.我将这个值传递给指令最接近的是:

<iframely url="'{{iterator.url}}'"></iframely> // note double and single quotes

这解决了迭代器的URL参数,而且还将其作为字符串的一部分与“单引号”一起传递.

编辑:也尝试没有单引号.

<iframely url="{{iterator.url}}"></iframely>

并得到错误:[$parse:Syntax]语法错误:从[{iterator.url}}开始,表达式[{{iterator.url}}的第2列的令牌'{‘无效键]

这样做的正确方法是什么?

EDIT2:这是指令的代码:

angular.module( 'iframely',[])

.directive( 'iframely',[ '$http','$sce',function ( $http,$sce ) {
    return {
        replace: true,restrict: "E",scope: {
            url: '='
        },template: '<div ng-bind-html="content"></div>',link: function ( scope,element,attrs ) {
            $http( {
                url: 'http://localhost:8061/iframely',method: 'GET',params: {
                    url: attrs.url
                }
            })
            .then( function ( result ) {
                scope.content = $sce.trustAsHtml( result.data.html )
            })
        }
    }
}])
你必须替换url:’=’

通过url:’@’

https://docs.angularjs.org/api/ng/service/$compile

Browserify,Babel 6,Gulp-点差运算符上的意外令牌

Browserify,Babel 6,Gulp-点差运算符上的意外令牌

我正在尝试让我的Browserify / Babelify / Gulp在我的项目中运行,但不会使用散布运算符。

我从gulpfile中收到此错误:

[SyntaxError: /Users/mboutin2/Desktop/Todo-tutorial/src/reducers/grocery-list-reducers.js: Unexpected token (16:8) while parsing file: /Users/mboutin2/Desktop/Todo-tutorial/src/reducers/grocery-list-reducers.js]

这是我的gulpfile.js

var gulp = require('gulp');
var source = require('vinyl-source-stream');
var browserify = require('browserify');
var sourcemaps = require('gulp-sourcemaps');
var uglify = require('gulp-uglify');
var buffer = require('vinyl-buffer');
var babelify = require('babelify');

gulp.task('build',function () {
  return browserify({entries: './src/client/app.js',extensions: ['.js'],debug: true})
    .transform(babelify,{presets: ['es2015','react']})
    .bundle()
    .on('error',function (err) {
      console.error(err);
      this.emit('end');
    })
    .pipe(source('app.min.js'))
    .pipe(buffer())
    .pipe(sourcemaps.init({loadMaps: true}))
    .pipe(uglify())
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('./public/js'));
});

gulp.task('default',['build']);

我试图创建一个.babelrc文件,但是它做同样的事情。当我删除传播运算符时,我的脚本有效。

这是发生意外令牌的文件(非常简单)。

import utils from '../utils/consts';

const initialState = {
  itemList: [
    {name: 'Apple',type: 'Fruit'},{name: 'Beef',type: 'Meat'}
  ]
};

export function groceryList(state = initialState,action = {}) {

  switch(action.type) {

    case utils.ACTIONS.ITEM_SUBMIT:
      return {
        ...state,itemList: [
          ...state.itemList,{name: action.name,type: action.itemType}
        ]
      };

    default:
      return state;

  }
}

我不知道这有什么用,我在Github和Babel网站上的设置页面上读了一些问题,但我无法使其正常运行。

谁能告诉我如何正确处理此问题?谢谢

JSON + Node.js-意外令牌o

JSON + Node.js-意外令牌o

我刚刚在大学课程中开始使用node.js和json对象。本周我们的任务之一是创建一些json对象,并将对象的一部分提取到html页面中。我以为自己对此有很好的掌握,但是尝试启动节点时遇到了错误。如果我删除了colleges对象和parse语句,则节点运行正常。

这是我运行“ node index.js”时遇到的错误:

undefined:1
[object Object],[object Object],[object Object
 ^
SyntaxError: Unexpected token o
at Object.parse (native)
at Object.<anonymous> (/home/ubuntu/node_stuff/node_json/requestHandlers.js:13:20)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/home/ubuntu/node_stuff/node_json/index.js:3:23)
at Module._compile (module.js:449:26)

这是我正在使用的代码。

var querystring = require("querystring"),fs = require("fs"),formidable = require("formidable");

var colleges = [
{"name":"A-B Tech","street":"340 Victoria Road","city":"Asheville","state":"NC","zip":"28801","phone":"828-254-1921"},{"name":"UNC Asheville","street":"1 University Heights","zip":"28804","phone":"828-251-6600"},{"name":"UNC Charlotte","street":"9201 University City Blvd","city":"Charlotte","zip":"28223","phone":"704-687-8622"},{"name":"Western Carolina","street":"North Carolina 107","city":"Cullowhee","zip":"28723","phone":"877-928-4968"},{"name":"NC State","street":"2200 Hillsborough","city":"Raleigh","zip":"27695","phone":"919-515-2011"}
];

var college = JSON.parse(colleges);

function abtech(response) {
console.log("Request handler 'abtech' was called.");

var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<ul>'+
'<li>' + college[0].name + '</li>'+
'<li>' + college[0].street + '</li>'+
'<li>' + college[0].city + ' ' + college[0].state + ' ' + college[0].zip + '</li>'+
'<li>' + college[0].phone + '</li>'+
'</ul>'+
'</body>'+
'</html>';

response.writeHead(200,{"Content-Type": "text/html"});
response.write(body);
response.end();
}

function unca(response) {
console.log("Request handler 'abtech' was called.");

var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<ul>'+
'<li></li>'+
'<li></li>'+
'<li></li>'+
'<li></li>'+
'</ul>'+
'</body>'+
'</html>';

response.writeHead(200,{"Content-Type": "text/html"});
response.write(body);
response.end();
}

function home(response) {
console.log("Request handler 'home' was called.");

var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<h1>Welcome to College</h2>'+
'<p>Where would you like to visit?</p>'+
'<ul>'+
'<li><a href="/colleges">Colleges</a></li>'+
'<li><a href="/hours">Hours of Operation</a></li>'+ 
'<li><a href="/start">Upload a Photo</a></li>'+
'<li><a href="/show">View Gallery</a></li>'+
'</ul>'+
'</body>'+
'</html>';

response.writeHead(200,{"Content-Type": "text/html"});
response.write(body);
response.end();
}

function colleges(response) {
console.log("Request handler 'colleges' was called.");

var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<h1>Colleges</h2>'+
'<ul>'+
'<li><a href="/abtech">A-B Tech</a></li>'+
'<li><a href="/unca">UNC Asheville</a></li>'+
'<li><a href="/uncc">UNC Charlotte</a></li>'+
'<li><a href="/wcu">Western Carolina</a></li>'+
'<li><a href="/ncsu">NC State</a></li>'+
'</ul>'+
'</body>'+
'</html>';

response.writeHead(200,{"Content-Type": "text/html"});
response.write(body);
response.end();
}

function hours(response) {
console.log("Request handler 'gallery' was called.");

var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<h1>Hours of Operation</h2>'+
'<table>'+
'<tr><td>Monday - Thursday</td><td>9 a.m. - 7 p.m.</td></tr>'+
'<tr><td>Friday</td><td>9 a.m. - 5 p.m.</td></tr>'+
'<tr><td>Saturday</td><td>9 a.m. - 12 p.m.</td></tr>'+
'</table>'+
'</body>'+
'</html>';

response.writeHead(200,{"Content-Type": "text/html"});
response.write(body);
response.end();
}


function start(response) {
console.log("Request handler 'start' was called.");

var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<h1>Upload a file</h2>'+
'<p>It will be shown on the /show url after</p>'+
'<form action="/upload" enctype="multipart/form-data" '+
'method="post">'+
'<input type="file" name="upload" multiple="multiple">'+
'<input type="submit" value="Upload file" />'+
'</form>'+
'</body>'+
'</html>';

response.writeHead(200,{"Content-Type": "text/html"});
response.write(body);
response.end();
}

function upload(response,request) {
console.log("Request handler 'upload' was called.");

var form = new formidable.IncomingForm();
console.log("about to parse");
form.parse(request,function(error,fields,files) {
console.log("parsing done");

/* Possible error on Windows systems:
   tried to rename to an already existing file */
fs.rename(files.upload.path,"/home/ubuntu/node_stuff/node_assignment/test.jpg",function(err) {
  if (err) {
    fs.unlink("/home/ubuntu/node_stuff/node_assignment/test.jpg")
    fs.rename(files.upload.path,"/home/ubuntu/node_stuff/node_assignment/test.jpg");
  }
});
response.writeHead(200,{"Content-Type": "text/html"});
response.write("received image:<br/>");
response.write("<img src='/show' />");
response.end();
});
}

function show(response) {
console.log("Request handler 'show' was called.");
fs.readFile("/home/ubuntu/node_stuff/node_assignment/test.jpg","binary",file) {
if(error) {
  response.writeHead(500,{"Content-Type": "text/plain"});
  response.write(error + "\n");
  response.end();
} else {
  response.writeHead(200,{"Content-Type": "image/jpg"});
  response.write(file,"binary");
  response.end();
}
});
}

exports.start = start;
exports.upload = upload;
exports.show = show;
exports.home = home;
exports.colleges = colleges;
exports.hours = hours;
exports.abtech = abtech;

任何有关我做错事情的提示将不胜感激。我的教练很难在周末得到帮助,所以我真的没有其他可转向的地方。谢谢。

关于JSON中位置4上的意外令牌<的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Angular 2 SyntaxError:JSON.parse()中位置0的JSON中的意外标记<、angularjs – 语法错误:令牌’:’是将变量传递给指令时的意外令牌、Browserify,Babel 6,Gulp-点差运算符上的意外令牌、JSON + Node.js-意外令牌o等相关知识的信息别忘了在本站进行查找喔。

本文标签: