开发者

jquery.post() getting and returning php jQuery

开发者 https://www.devze.com 2023-02-11 11:47 出处:网络
I am doing next... request in php from html (jQuery) $.post(\"index.php?res=ok\", { username: user, userpass: pass }, function(data) {

I am doing next...

request in php from html (jQuery)

$.post("index.php?res=ok", { username: user, userpass: pass }, function(data) {
    $("#signin_ok").html("You sign in now "+data.name+". Your password is: "+data.pass);
    $开发者_运维技巧("#signin_ok").show()
    .animate({fontSize:"120%"}, 100).css({color:"#32CD32"})
    .animate({fontSize:"100%"}, 100)
    .animate({fontSize:"100%"}, 3000)
    .animate({fontSize:"120%"}, 200, function() {
        $(this).css({display:"none"});
    });
});

return from php in html (jQuery)

if(isset($_REQUEST['res']) && $_REQUEST['res'] == "ok") {
    if(isset($_POST['username']) && isset($_POST['userpass'])) {
        $username = $_POST['username'];
        $userpass = $_POST['userpass'];
        echo json_encode(array("name" => $username, "pass" => $userpass));
    }
}

but returns "You sign in now undefined. Your password is: undefined"

What's wrong? How to fix it?

P.S.

When I add "json" like this

$.post("index.php?res=ok", { username: user, userpass: pass }, function(data) {
    . . . . . . 
    . . . . . .
}, "json");

no reaction

What was going on???


Try adding the JSON headers to the PHP script with

header('Content-type: application/json');
// then your code

Also, seems your just returning the passed data, why don't you just use user and pass as the data, rather than data.user and data.pass?


Does this do the trick?

$.post("index.php?res=ok", { username: user, userpass: pass }, function(data) {
    $("#signin_ok").html("You sign in now "+data[0].name+". Your password is: "+data[0].pass);
    $("#signin_ok").show()
    .animate({fontSize:"120%"}, 100).css({color:"#32CD32"})
    .animate({fontSize:"100%"}, 100)
    .animate({fontSize:"100%"}, 3000)
    .animate({fontSize:"120%"}, 200, function() {
        $(this).css({display:"none"});
    });
});


I'll respond with a $.ajax call because that tends to be what I use, since $.post just wraps $.ajax anyways...

$.ajax({

     type: "POST",
     url: "index.php?res=ok",
     data: { "username" : user, "userpass": pass },
     success: function(msg) {

          var data = $.parseJSON(msg);
          alert(data.username);
          alert(data.userpass);

     }

});


I have solved this problem myself, I think so :)

I have created new file (new.php):

<?php
if(isset($_REQUEST['res']) && $_REQUEST['res'] == "ok") {
    if(isset($_POST['username']) && isset($_POST['userpass'])) {
        $username = $_POST['username'];
        $userpass = $_POST['userpass'];
        $arr = array("name" => $username, "pass" => $userpass);
        echo json_encode(array("name" => $username, "pass" => $userpass));
    }
}
?>

and passed request from HTML to PHP (new.php)

. . .
$.post("new.php", { "username": user, "userpass": pass, "res": "ok" }, function(data) {
    $("#signin_ok").html("You sign in now "+data['name']+". Your password is: "+data['pass']);
    console.log(data);
    $("#signin_ok").show()
        .animate({fontSize:"120%"}, 100).css({color:"#32CD32"})
        .animate({fontSize:"100%"}, 100)
        .animate({fontSize:"100%"}, 3000)
        .animate({fontSize:"120%"}, 200, function() {
            $(this).css({display:"none"});
        });
}, "json");
. . .

and I got result :) but it's stupid, why it's doesn't work in index.php???

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号