简体   繁体   中英

jquery not sending parameters to php

I am running into an issue where jquery is not sending parameters at all. I looped through the $_POST and $_REQUEST and neither one contained my information.

//jquery client side
$.post(http://www.example.com/myscript.php,{"data":"1234"})
.done(function(info){alert("returned data "+info);});

//myscript.php server side
<?php
foreach($_POST as $val){
echo $val;
}
?>

i have also tried $_REQUEST, I've tried $.get. I dont know what I'm doing wrong. If I call the php code in the url bar ( http://www.example.com/myscript.php?data=1234 ) when it uses $_REQUEST it works. I tried putting the url in quotes and that didn't work, I tried taking the 1234 out of the quotes and that didn't work. I have looked through just about every jquery question on SO and didn't find something that works but I may have missed something. Any help is appreciate.

//jquery client side

// missing quotes around url

$.post("http://www.example.com/myscript.php",{"data","1234"})
.done(function(info){alert("returned data "+info);});

Can you try using this,

    $.post( "http://www.example.com/myscript.php", { data: "1234"} ).done(function( info ) {
        alert("returned data "+info);
    });

Ref: http://api.jquery.com/jQuery.post/

JavaScript中有{“ data”,“ 1234”},正确的是:

{"data":"1234"}

I'd like to suggest an alternative if I may...

$.ajax({
    type: 'POST',
    url: 'http://www.example.com/myscript.php',
    data: { data: "1234" },
    success: function doAfterPost() {
        // do this after the post has been sent
    }
});

This should send $_POST["data"] to the specified php file with a string value of 1234 .

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