这是代码,所以我得到了结果:
$.ajax({
url: location.origin+'/getcat?t='+id,
dataType:"json"
}).success(function(data){
var array = data.data;
array.ForEach(function(print){
console.log(print.title);
});
我有一个像这样的ajax回调:
这是错误消息:
Uncaught TypeError: array.ForEach is not a function
at Object.<anonymous> (blogs:811)
at j (jquery-2.1.1.min.js:2)
at Object.fireWith [as resolveWith] (jquery-2.1.1.min.js:2)
at x (jquery-2.1.1.min.js:4)
at XMLHttpRequest.<anonymous> (jquery-2.1.1.min.js:4)
我尝试使用data.ForEach(function(){});
打印出所有data
但错误。我知道这应该可以用php foreach()
完成,但我需要在javascript中完成。怎么做?
答案 0 :(得分:0)
提高开发技能的三个提示:
首先,你说你有错误。只需阅读错误,您就会知道问题(大多数情况下错误都足够冗长)。其次,如果你没有线索,那么你应该在这里分享错误,所以他们有额外的数据可以帮助发现问题。第三,似乎你有缩进问题,这可能导致难以理解的代码。
这里的问题是import sys
from tkinter import messagebox
from tkinter import *
now = open("pass.txt","w+")
now.write("user\n")
now.write("python3")
now.close()
def login_in():
with open("pass.txt") as f:
new = f.readlines()
name = new[0].rstrip()
password = new[1].rstrip()
if entry1.get() == name and entry2.get() == password:
root.deiconify()
log.destroy()
else:
messagebox.showerror("error","login Failed")
def change_login():
ch = Toplevel(root)
ch.geometry('300x300')
e1 = Entry(ch, width=20).pack()
e2 = Entry(ch, width=20).pack()
sb = Button(ch, text="save", command=save_changes).pack()
def save_changes(): # function to change data in the txt file
data = e1.get() + "\n " + e2.get()
with open("pass.txt", "w") as f:
f.writelines(data)
root= Tk()
log = Toplevel()
root.geometry("350x350")
log.geometry("200x200")
entry1 = Entry(log)
entry2 = Entry(log)
button1 = Button(log, text="Login", command=login_in) #Login button
entry1.pack()
entry2.pack()
button1.pack()
label = Label(root, text="welcome").pack()
butt = Button(root, text="change data in file", command=change_login).pack()
root.withdraw()
root.mainloop()
必须以小写字母开头,因为JavaScript使用Lower Camel Case,所以:
ForEach