简体   繁体   中英

Cannot get Results from PHP using JQuery Post

sorry I am an amateur coder and learning this as a hobby. I am not able to get results of the php back to the javascript after I $.post.

  1. I know test.php has ran properly with the "Name" passed through, as I've put an SQL update to see the name and time is updated every time I click this button.

  2. The page itself, test.php, runs fine on it's own with echo results

  3. I just cannot get the result into data and for alert(data) to work.

Edit: My problem was as Reyaner has pointed out, it will not work on different domains. Is there any work around?

Javascript

$("button").click(function(){
        $.post(
                "http://www.test.com/test.php",  // works
                {name : "Bozmo"},  // works
                function(data){   // does not work !!
                        alert("Data: " + data);
                }
        );
});

PHP

$name = $_POST["name"]; 
echo $name."is here";

Thanks in Advance, Jimmy

$("button").click(function(){ 
 $.ajax({
        url: "test.php",
        type: "post",
        data: {name : "Bozmo"},
        success: function(data){
            alert("Data: " + data);
        },
        error:function(){
            alert("failure");        
        }
    });
});

Use :

$.post("http://www.test.com/test.php", { name : "Bozmo" })
.done(function(data) {
alert("Data: " + data);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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