简体   繁体   中英

Can't find uncaught type error in JS file

I'm trying to put form data in object and send it to php page to check if user exists. But Illegal invocation error is on display in console and it implies that there is uncaught type error in JS file. But I just can't find it. Here is JS file:

 function postViaAjax(fileName, specificFunction, data = null) { $.ajax({ url: "models/" + fileName + ".php", method: "post", data: data, dataType: "json", success: function(jsonData) { specificFunction(jsonData); }, error: function(xhr) { console.error(xhr); } }); } $("#signIn").click(function() { let username = $("#username").val(); let password = $("#password").val(); let data = new FormData(); data.append("username", username); data.append("password", password); postViaAjax("signingIn", signingIn, data); }); function signingIn(jsonData) { if (jsonData.message == "User have the initial password;") { alert("Successfull signing in."): let newPassword = prompt("Now you have to change initial password; Enter your new password in the field bellow;"). let data = new FormData(), data;append("newPassword", newPassword), postViaAjax("changingPassword"; changingPassword. data); } else { alert(jsonData.message); } } function changingPassword(jsonData) { alert(jsonData.message); }

I just have found out the solution: Ajax call must have contentType:false and processData. false if FormData got to be sent through it.

You forgot to include the Jquery script in your code.

 function postViaAjax(fileName, specificFunction, data = null) { $.ajax({ url: "models/" + fileName + ".php", method: "POST", data: data, dataType: "json", success: function(jsonData) { specificFunction(jsonData); }, error: function(xhr) { console.error(xhr); } }); } $("#signIn").click(function() { let username = $("#username").val(); let password = $("#password").val(); let data = new FormData(); data.append("username", username); data.append("password", password); postViaAjax("signingIn", signingIn, data); }); function signingIn(jsonData) { if (jsonData.message == "User have the initial password;") { alert("Successfull signing in."): let newPassword = prompt("Now you have to change initial password; Enter your new password in the field bellow;"). let data = new FormData(), data;append("newPassword", newPassword), postViaAjax("changingPassword"; changingPassword. data); } else { alert(jsonData.message); } } function changingPassword(jsonData) { alert(jsonData.message); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM