内容简介:翻译自:https://stackoverflow.com/questions/18862066/jquery-ajax-always-returns-error-data-being-added-to-database
ajax调用将用户添加到数据库.用户可以很好地添加到数据库中,但是ajax总是返回错误.我不知道如何检索特定的错误.下面是我的代码,表单,php和jquery.
这是jquery
$(document).ready(function() { //ajax call for all forms. $('.button').click(function() { var form = $(this).closest('form'); $.ajax({ type: "POST", url: form.attr('data'), dataType: 'json', data: form.serialize(), success: function (response) { alert('something'); }, error: function() { alert('fail'); } }); }); });
这是PHP
<?php include 'class_lib.php'; if(isset($_POST['username'])) { $user = new Users; $user->cleanInput($_POST['username'], $_POST['password']); if($user->insertUser()) { echo json_encode('true'); } else { echo json_encode('false'); } }
这是HTML
<div id='newUser' class='tool'> <h3>New User</h3> <form method='post' name='newUser' data='../php/newUser.php'> <span>Username</span><input type='text' name='username'><br> <span>Password</span><input type='password' name='password'> <input type='submit' name='submit' class='button' style='visibility: hidden'> </form> <span class='result'> </span> </div>
@Musa,你刚才提到的
My guess is its a parsing error, try removing dataType: ‘json’, and see if it works
你完全解决了我遇到的问题!我的ajax帖子请求与上面类似,它只是继续返回“错误”部分.虽然我使用firebug进行了检查,但状态为200(ok)并且没有错误.
删除’dataType:json’为我解决了这个问题.非常感谢!
翻译自:https://stackoverflow.com/questions/18862066/jquery-ajax-always-returns-error-data-being-added-to-database
以上所述就是小编给大家介绍的《javascript – jquery .ajax总是返回错误 – 数据被添加到数据库》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- Vue添加数据视图不更新问题
- Flask web模板四–添加数据库模块
- jsp 使用request为页面添加静态数据的实例
- oracle 表空间文件达到32G后手动添加数据文件
- Spring Boot 添加应用 JVM 监控 ,实现数据可视化
- Spring Boot 添加应用 JVM 监控,实现数据可视化
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Web Designer's Idea Book
Patrick Mcneil / How / 2008-10-6 / USD 25.00
The Web Designer's Idea Book includes more than 700 websites arranged thematically, so you can find inspiration for layout, color, style and more. Author Patrick McNeil has cataloged more than 5,000 s......一起来看看 《The Web Designer's Idea Book》 这本书的介绍吧!