.ajax() 在 jquery 中不起作用


.ajax() wont work in jquery

我是jquery的新手。我正在尝试从jquery使用ajax()向我的php发送电子邮件,然后将其返回我的jquery。但是该程序不起作用。

$.post(..) 在 jquerying.js 文件中的 valid() 函数中工作正常,但 $.ajax(...) 不是。我想在 jquerying 中的 valid() 函数中制作 $.ajax(....js 工作

jquerying.js 文件中 valid() 函数中的 $.ajax(... ) 存在问题

这是我的 html 文件:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charse="utf-8">
<title> jquerying </title>
<link rel="stylesheet" type="text/css" href="jquerying.css"/>   
</head>
<body>
<br/>   
<form>
Email: <input id="em" type="email"/> </input>
<input id="sub" type="submit"> </input>
<div id="emailf"> </div>
</form>
<script type="text/javascript" src="jquery.js"> </script>
<script type="text/javascript" src="jquerying.js"> </script>
</body>
</html>

我的jquerying.js文件如下:

$(document).ready(function()
{
$('#em').focusin(function()
{
    if($(this).val()=='')
    {
        $('#emailf').html('Enter your email');
    }
    else
    {
        valid($(this).val());
    }
}).blur(function()
{
    $('#emailf').html('');
}).keyup(function()
{
    if($(this).val()!='')
    {
        $('#emailf').html('');
        valid($(this).val());
    }
    else
    {
        $('#emailf').html('Enter your email');
    }
});
});
function valid(emailer)
{
$.ajax({url: 'email.php', type: 'POST', data: {emailer: emailer},
       function(info)
       {
            $('#emailf').html(info);
       }
       });
/*  $.post('email.php', {data: emailer}, function(info)
{
    $('#emailf').html(info);
}); */
}

在 jquerying.js 文件中的 valid() 函数中,$.post( ...) 工作正常!

我的电子邮件.php是:

<?php
if(isset($_POST['emailer']))
{
    $email= $_POST['emailer'];
    echo $email;
}       
?>

AJAX 调用的语法看起来不正确。这是文档

这是您的通话应该是什么样子

$.ajax({
    url: 'email.php',
    type: 'POST',
    data : { emailer : emailer }
})
.done(function (data) {
    console.log("success", data);
    $('#emailf').html(data);
})
.fail(function (data) {
    console.log("error", data);
})