简体   繁体   English

如何使用ajax在jquery中发送发布数据

[英]how to send post data in jquery using ajax

i have a simple form and i want that the as the input field losses the focus the event is fired which will check the content if and replies accordingly to that 我有一个简单的表格,我希望当输入字段失去焦点时,将触发事件被触发,事件将检查内容是否存在并对此做出相应答复

$("document").ready(function()
    $("#txt_Email").blur(function() {                   
        var email=$("#txt_Email").val();                   
        $.post('ajax/tester.php', {chk:email}, function(data) {
            alert(data) ;
        });
    });
}); 

so i have done till this but it's showing nothing: no error, no output. 所以我做完为止,但是它什么也没显示:没有错误,没有输出。 i am using jquery 1.9.1 and i also want to know how to check the value of post data on the ajax/tester.php page 我正在使用jquery 1.9.1 ,我也想知道如何检查ajax/tester.php页面上发布数据的值

Right now i am doing this 现在我正在这样做

$tochk=isset($_POST['chk'])?$_POST['chk']:null;

echo $tochk;

I hope this helps: 我希望这有帮助:

$(function(){
    $("#txt_Email").blur(function() {                   
        var email=$("#txt_Email").val(); 
        alert('email: ' + email);
        var jqxhr = $.post('ajax/tester.php', {chk:email});
        jqxhr.done(function(data) { alert(data); });
        jqxhr.fail(function() { alert("error"); });
    });
});

window.onerror = function(errorMessage, url, line) {
    var errorText = 'message: ' + errorMessage + '\nurl: ' + url + '\nline: ' + line;
    alert(errorText);
}

Change this line: 更改此行:

$("document").ready(function()

to: 至:

$(document).ready(function()

You need to refer to the document object, not the string "document" . 您需要引用document对象,而不是字符串"document" Using your same code with this small change it works fine. 使用您的相同代码进行此小更改,效果很好。 Here's an example: http://jsfiddle.net/YjEMd/ . 这是一个示例: http : //jsfiddle.net/YjEMd/ Note that this example has change the URL of the Ajax and the format of the data being posted so that it can work inside jsFiddle's sandbox. 请注意,此示例更改了Ajax的URL和要发布的数据的格式,以便可以在jsFiddle的沙箱中使用。 Also the $(document).ready(...) bit is done for you in the background. $(document).ready(...)位也会在后台为您完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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