AJAX在jQuery中有效,但在香草JS中无效

JTrixx16

我有一个小表格,其中当用户名字段模糊或不清晰时,AJAX会检查输入的用户名是否已记录在数据库中。问题在于,jQuery中的AJAX可以工作,而香草JS中的AJAX不能工作。我需要知道我的错误代码行或部分内容。

的HTML

<form action="process.php" method="post">
    <input class="username" type="text" name="username" placeholder="Enter username" autocomplete="off">
    <span class="uname_notice"></span><br>
    <input type="submit" value="Submit">
</form>

jQuery的

$('.username').blur(function() {
    var username = $(this).val();

    $.ajax({
        url: 'process.php',
        type: 'post',
        data: {username: username},
        success: function(responseText) {
            $('.uname_notice').text(responseText);
        }
    })
});

香草JS

document.querySelector('.username').onblur = function() {
    var xhr = new XMLHttpRequest();

    xhr.open("POST", "process.php", true);
    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4 && xhr.status == 200) {
            document.querySelector('.uname_notice').textContent = xhr.responseText;
        }
    }
    xhr.send();
}
舒巴姆·哈特里(Shubham Khatri)

与HTTP请求一起发送数据。

document.querySelector('.username').onblur = function() {
    var xhr = new XMLHttpRequest();

    var data = new FormData();
    var username = document.getElementsByClassName('username')[0].value;
    data.append('username', username);
    xhr.open("POST", "process.php", true);
    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4 && xhr.status == 200) {
            document.querySelector('.uname_notice').textContent = xhr.responseText;
        }
    }
    xhr.send(data);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

jQuery在控制台中有效,但在链接的js文件中无效

来自分类Dev

查询在sql中有效,但在php中无效

来自分类Dev

regEx在notepad ++中有效,但在python中无效

来自分类Dev

orphanRemoval在PostgreSQL中有效,但在hsqldb中无效

来自分类Dev

代码在终端中有效,但在脚本中无效

来自分类Dev

orphanRemoval在PostgreSQL中有效,但在hsqldb中无效

来自分类Dev

WritePrivateProfileString 在 main 中有效但在函数中无效

来自分类Dev

字符串修改在jQuery 1.6.4中有效,但在1.3.2中无效

来自分类Dev

jQuery Bootstrap在Firefox中有效,但在IE10中无效

来自分类Dev

$ _POST和javascript –在javascript中有效,但在jquery插件中无效

来自分类Dev

jQuery Click 功能在 Firefox 中有效,但在 Chrome/Safari 中无效

来自分类Dev

jQuery 脚本在 HTML 中有效,但在外部文件中无效

来自分类Dev

mysql查询在phpmyadmin中有效,但在node.js中无效

来自分类Dev

该示例在js小提琴中有效,但在自制页面中无效

来自分类Dev

Http 请求在 PostMan 中有效,但在 JS 中无效

来自分类Dev

为什么相同的 POST 请求在 POSTMAN 中有效,但在浏览器 AJAX 中无效(404 Not Found)?

来自分类Dev

三个 js - JSON 加载程序在示例中有效但在我的代码中无效?

来自分类Dev

设置值jquery在跟踪中有效,但在没有跟踪时无效

来自分类Dev

更新集命令在 Access 中有效,但在带有 @parameters 的 Visual Studio 中无效

来自分类Dev

JS 和 Bootstrap 在 Codepen 中有效,但在本地打开时无效

来自分类Dev

本地化在iOS 7中有效,但在iOS 8中无效

来自分类Dev

代码在Visual Studio 2010中有效,但在Visual Studio 2015中无效

来自分类Dev

Popover在iOS 8中有效,但在iOS 7中无效

来自分类Dev

对象!= nil在Beta 5中有效,但在Beta 6中无效吗?

来自分类Dev

import pyttsx在python 2.7中有效,但在python3中无效

来自分类Dev

Set-ItemProperty在PowerShell 5.1中有效,但在7.0中无效

来自分类Dev

jarsigner -verify在Java 6中有效,但在Java 7中无效

来自分类Dev

宏在Word 2013中有效,但在2010年中无效

来自分类Dev

内联在 CSS 中的 SVG 在 Chrome/Edge 中有效,但在 Firefox 中无效

Related 相关文章

  1. 1

    jQuery在控制台中有效,但在链接的js文件中无效

  2. 2

    查询在sql中有效,但在php中无效

  3. 3

    regEx在notepad ++中有效,但在python中无效

  4. 4

    orphanRemoval在PostgreSQL中有效,但在hsqldb中无效

  5. 5

    代码在终端中有效,但在脚本中无效

  6. 6

    orphanRemoval在PostgreSQL中有效,但在hsqldb中无效

  7. 7

    WritePrivateProfileString 在 main 中有效但在函数中无效

  8. 8

    字符串修改在jQuery 1.6.4中有效,但在1.3.2中无效

  9. 9

    jQuery Bootstrap在Firefox中有效,但在IE10中无效

  10. 10

    $ _POST和javascript –在javascript中有效,但在jquery插件中无效

  11. 11

    jQuery Click 功能在 Firefox 中有效,但在 Chrome/Safari 中无效

  12. 12

    jQuery 脚本在 HTML 中有效,但在外部文件中无效

  13. 13

    mysql查询在phpmyadmin中有效,但在node.js中无效

  14. 14

    该示例在js小提琴中有效,但在自制页面中无效

  15. 15

    Http 请求在 PostMan 中有效,但在 JS 中无效

  16. 16

    为什么相同的 POST 请求在 POSTMAN 中有效,但在浏览器 AJAX 中无效(404 Not Found)?

  17. 17

    三个 js - JSON 加载程序在示例中有效但在我的代码中无效?

  18. 18

    设置值jquery在跟踪中有效,但在没有跟踪时无效

  19. 19

    更新集命令在 Access 中有效,但在带有 @parameters 的 Visual Studio 中无效

  20. 20

    JS 和 Bootstrap 在 Codepen 中有效,但在本地打开时无效

  21. 21

    本地化在iOS 7中有效,但在iOS 8中无效

  22. 22

    代码在Visual Studio 2010中有效,但在Visual Studio 2015中无效

  23. 23

    Popover在iOS 8中有效,但在iOS 7中无效

  24. 24

    对象!= nil在Beta 5中有效,但在Beta 6中无效吗?

  25. 25

    import pyttsx在python 2.7中有效,但在python3中无效

  26. 26

    Set-ItemProperty在PowerShell 5.1中有效,但在7.0中无效

  27. 27

    jarsigner -verify在Java 6中有效,但在Java 7中无效

  28. 28

    宏在Word 2013中有效,但在2010年中无效

  29. 29

    内联在 CSS 中的 SVG 在 Chrome/Edge 中有效,但在 Firefox 中无效

热门标签

归档