Javascript 语法错误?
Syntax Error on Javascript?
嘿,我做错了什么?我的 JS 文件停止工作,我尝试了不同的方法但它不起作用。我想通过 "id".
单击按钮来启动该功能
$(document).ready(function () {
$("#paybutton").click(function () {}
var params = ("projectpaymentoption=1197&id=");
var usernamepay = window.localStorage.getItem("username");
var paymenturl = params + usernamepay;
}
$.ajax({
type: 'post',
url: 'http://www.bla.de/phone/encode.php',
data: {
data: {
"usernamepay": usernamepay
},
}
success: function (result) {
console.log(result);
}
});
格式化代码如下。下次请使用一些IDE。
$(document).ready(function...
功能已关闭,底部为 });
。
$("#paybutton").click(function(){..
已通过 });
适当关闭
删除了额外的 data: {
声明,在 success: function()
之前放置了一个 ,
。还更正了其他缩进。
$(document).ready( function() {
$("#paybutton").click(function() {
var params = ("projectpaymentoption=1197&id=");
var usernamepay = window.localStorage.getItem("username");
var paymenturl = params + usernamepay;
$.ajax({
type: 'post',
url: 'http://www.bla.de/phone/encode.php',
data: {"usernamepay": usernamepay},
success: function(result) {
console.log(result);
}
});
});
});
您应该使用一些 IDE(或者至少一些可以帮助您进行语法着色和括号匹配的程序 - notepad++ 可能很棒)并在您的代码中添加一些缩进
这是你的缩进代码,以及我在此处添加的一些注释。
这样您就可以看到您的问题出在哪里以及如何解决它们。
$(document).ready( function() {
$("#paybutton").click(function() {
// Nothing here
} // <-- Here you should close the click function (from two lines above), missing )
var params = ("projectpaymentoption=1197&id=");
var usernamepay = window.localStorage.getItem("username");
var paymenturl = params + usernamepay;
}
$.ajax({
type: 'post',
url: 'http://www.bla.de/phone/encode.php',
data: {
data: {"usernamepay": usernamepay},
} // <-- Here you should have a comma. Elements of object are separated with commas, just like your previous lines
success: function(result) {
console.log(result);
}
});
// <-- Here you should close the ready function (from the first line), missing )
'success' 字段前需要一个逗号。如果在代码中不需要,但在数据对象文字中,它将是必需的。这是一个问题,但可能还有更多。
问题:
你没有关闭所有的括号,我假设你关闭点击回调函数太早了。你没有正确结束你的括号。您的 ajax 调用超出了点击处理程序和就绪函数的范围。
Note: You need to learn to use indentation, bracket placement and comments to help you see code blocks as different sections of the execution so you can spot things like the ajax call only being fired once (without the correct variables) rather than in that click handler that would allow it to happen multiple times.
您不需要在变量赋值中将字符串括起来。
编辑:下一部分是 angular 特定的,不是 jquery,但不是无效的
.ajax() 要求 post 请求中的数据为字符串格式,而不是对象格式。
解决方案:
重新组织代码以显示缩进并更正闭包,包括结尾 });这是需要的。
删除了括号,并将变量声明合并到一个 var 语句中(可读性和效率/文件大小)
也是个好主意
我使用 $.param(obj)
来格式化传递的数据。
代码:
'use strict';
$(document).ready( function() {
$("#paybutton").click(function() {
var params = "projectpaymentoption=1197&id=",
usernamepay = window.localStorage.getItem("username"),
paymenturl = params + usernamepay;
$.ajax({
type: 'POST',
url: 'http://www.bla.de/phone/encode.php',
data: $.param({"usernamepay": usernamepay}),
success: function(result) {
console.log(result);
}
}); // end of ajax call
}); // end of #payButton click handler
}); // end of document.ready function
嘿,我做错了什么?我的 JS 文件停止工作,我尝试了不同的方法但它不起作用。我想通过 "id".
单击按钮来启动该功能$(document).ready(function () {
$("#paybutton").click(function () {}
var params = ("projectpaymentoption=1197&id=");
var usernamepay = window.localStorage.getItem("username");
var paymenturl = params + usernamepay;
}
$.ajax({
type: 'post',
url: 'http://www.bla.de/phone/encode.php',
data: {
data: {
"usernamepay": usernamepay
},
}
success: function (result) {
console.log(result);
}
});
格式化代码如下。下次请使用一些IDE。
$(document).ready(function...
功能已关闭,底部为 });
。
$("#paybutton").click(function(){..
已通过 });
删除了额外的 data: {
声明,在 success: function()
之前放置了一个 ,
。还更正了其他缩进。
$(document).ready( function() {
$("#paybutton").click(function() {
var params = ("projectpaymentoption=1197&id=");
var usernamepay = window.localStorage.getItem("username");
var paymenturl = params + usernamepay;
$.ajax({
type: 'post',
url: 'http://www.bla.de/phone/encode.php',
data: {"usernamepay": usernamepay},
success: function(result) {
console.log(result);
}
});
});
});
您应该使用一些 IDE(或者至少一些可以帮助您进行语法着色和括号匹配的程序 - notepad++ 可能很棒)并在您的代码中添加一些缩进
这是你的缩进代码,以及我在此处添加的一些注释。
这样您就可以看到您的问题出在哪里以及如何解决它们。
$(document).ready( function() {
$("#paybutton").click(function() {
// Nothing here
} // <-- Here you should close the click function (from two lines above), missing )
var params = ("projectpaymentoption=1197&id=");
var usernamepay = window.localStorage.getItem("username");
var paymenturl = params + usernamepay;
}
$.ajax({
type: 'post',
url: 'http://www.bla.de/phone/encode.php',
data: {
data: {"usernamepay": usernamepay},
} // <-- Here you should have a comma. Elements of object are separated with commas, just like your previous lines
success: function(result) {
console.log(result);
}
});
// <-- Here you should close the ready function (from the first line), missing )
'success' 字段前需要一个逗号。如果在代码中不需要,但在数据对象文字中,它将是必需的。这是一个问题,但可能还有更多。
问题:
你没有关闭所有的括号,我假设你关闭点击回调函数太早了。你没有正确结束你的括号。您的 ajax 调用超出了点击处理程序和就绪函数的范围。
Note: You need to learn to use indentation, bracket placement and comments to help you see code blocks as different sections of the execution so you can spot things like the ajax call only being fired once (without the correct variables) rather than in that click handler that would allow it to happen multiple times.
您不需要在变量赋值中将字符串括起来。
编辑:下一部分是 angular 特定的,不是 jquery,但不是无效的 .ajax() 要求 post 请求中的数据为字符串格式,而不是对象格式。
解决方案:
重新组织代码以显示缩进并更正闭包,包括结尾 });这是需要的。
删除了括号,并将变量声明合并到一个 var 语句中(可读性和效率/文件大小)
也是个好主意我使用 $.param(obj)
来格式化传递的数据。
代码:
'use strict';
$(document).ready( function() {
$("#paybutton").click(function() {
var params = "projectpaymentoption=1197&id=",
usernamepay = window.localStorage.getItem("username"),
paymenturl = params + usernamepay;
$.ajax({
type: 'POST',
url: 'http://www.bla.de/phone/encode.php',
data: $.param({"usernamepay": usernamepay}),
success: function(result) {
console.log(result);
}
}); // end of ajax call
}); // end of #payButton click handler
}); // end of document.ready function