错误,当我宣布
我使用的WebPack建立自己的项目中的AJAX功能,但问题是,当我宣布一个Ajax调用函数象下面这样:错误,当我宣布
<script>
var self = this;
login(){
$.ajax({
url: '/feedback',
type: 'POST',
data: params,
success: function(res){
if(res == 'OK'){
alert("Received your message. We shall get back to your shortly.");
}
},
error: function(err){
console.log('error: ', err);
};
})
}
</script>
我得到错误:
ERROR in ./public_html/tags/login_tag.tag
Module parse failed: E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\riotjs-loader\index.js?{}!E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\public_html\tags\login_tag.tag Unexpected token (16:17)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (16:17)
at Parser.pp$4.raise (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:2221:15)
at Parser.pp.unexpected (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:603:10)
at Parser.pp.expect (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:597:28)
at Parser.pp$3.parseObj (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1973:16)
at Parser.pp$3.parseExprAtom (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1805:19)
at Parser.pp$3.parseExprSubscripts (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1715:21)
at Parser.pp$3.parseMaybeUnary (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1692:19)
at Parser.pp$3.parseExprOps (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1637:21)
at Parser.pp$3.parseMaybeConditional (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1620:21)
at Parser.pp$3.parseMaybeAssign (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1597:21)
at Parser.pp$3.parseExprList (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:2165:22)
at Parser.pp$3.parseSubscripts (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1741:35)
at Parser.pp$3.parseExprSubscripts (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1718:17)
at Parser.pp$3.parseMaybeUnary (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1692:19)
at Parser.pp$3.parseExprOps (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1637:21)
at Parser.pp$3.parseMaybeConditional (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1620:21)
at Parser.pp$3.parseMaybeAssign (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1597:21)
at Parser.pp$3.parseExpression (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1573:21)
at Parser.pp$1.parseStatement (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:727:47)
at Parser.pp$1.parseBlock (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:981:25)
at Parser.pp$3.parseFunctionBody (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:2105:24)
at Parser.pp$1.parseFunction (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1065:10)
at Parser.pp$3.parseExprAtom (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1810:19)
at Parser.pp$3.parseExprSubscripts (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1715:21)
at Parser.pp$3.parseMaybeUnary (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1692:19)
at Parser.pp$3.parseExprOps (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1637:21)
at Parser.pp$3.parseMaybeConditional (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1620:21)
at Parser.pp$3.parseMaybeAssign (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1597:21)
at Parser.pp$3.parseMaybeAssign (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1608:25)
at Parser.pp$3.parseExpression (E:\WORKSPACE\NETBEANS_WORKSPACE\Mantra_Pushpam\node_modules\acorn\dist\acorn.js:1573:21)
@ ./index.js 7:0-43
注:当我删除了AJAX功能,还有在所有
这没有错误是我的WebPack配置:
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: './index.js',
output: {
path: __dirname,
filename: 'public_html/js/bundle.js'
},
resolveLoader: {
root: path.join(__dirname, 'node_modules')
},
module: {
preLoaders: [{
test: /\.tag$/,
exclude: /node_modules/,
loader: 'riotjs-loader',
query: {
//type: 'none'
//compact: true
}
}],
loaders: [
/*{
test: /\.tag$/,
loader: 'tag',
exclude: /node_modules/
},*/
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
//commenting because of this issue https://github.com/mapbox/mapbox-gl-js/issues/3422
// presets: ['es2015']
}
}
]
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.ProvidePlugin({
riot: 'riot',
})
]
};
你有一些畸形的JS那里 - 这就是我的猜想是你的问题:
var self = this;
login(){ // <== WHAT'S HAPPENING HERE? IS LOGIN SUPPOSED TO BE A FUNCTION DECLARATION?
$.ajax({
url: '/feedback',
type: 'POST',
data: params, // LESS CRITICAL, BUT I DON'T SEE params DEFINED ANYWHERE
success: function(res){
if(res == 'OK'){
alert("Received your message. We shall get back to your shortly.");
}
},
error: function(err){
console.log('error: ', err);
}; // <-- THERE SHOULDN'T BE A SEMICOLON HERE!!!!
})
} // <== THERE *MIGHT* NEED TO BE A SEMICOLON HERE, DEPENDING ON WHAT YOU'RE TRYING TO DO
这是es6语法。这不是问题 – kittu
@kittu:只在对象初始值设定项或类中,而不是像你在这里那样是独立的。你在这里试图调用*'login',然后是一个包含你的ajax代码的独立块。问题是分号anied指出。 –
如果你的webpack正在转换它或者浏览器可以处理它,那就好了。尽管如此,在传递给'ajax'方法的配置中肯定不应该有分号。 –
可能重复:您可能需要适当的加载器来处理此文件类型(HTTP: //www.stackoverflow.com/questions/33469929/you-may-need-an-woole-loader-to-handle-this-file-type-with-webpack-and-b) –
@TânNguyễn有很多不同类型的处理程序。相同的配置与我的其他项目工作正常。 – kittu
'login(){' epascarello