简体   繁体   中英

How to pass data to another page using jquery ajax?

What am I doing wrong ? I have jquery var which is storing data, i'm then sending this data to functions.php. But i'm not getting any results from the functions.php page. Any suggestions ?

var data = "&insertPost=1&title=" + title + "&content=" + content;

$.ajax({
    type:   'POST',
    cache:    false,
    url:      'functions.php',
    data:     data,
    success:  function( data ) {
        alert('wooo');      
    }

});

Then on my functions page.

if ($_POST['insertPost'] == true)
{
    $postTitle = $_POST['title'];
    $postContent = $_POST['content'];

    echo "Inserted";
}
else 
{
    return "No Data"; 
}

return $postData;

Try this,

in ajax data part(this is for POST)

data:{insertPost:1,title:title,content:content},

For GET

var data = "insertPost=1&title=" + title + "&content=" + content;
           ^// no & here
type:   'GET',
url:      'functions.php/'+data,

If you want to send back any data from ajax call, return will not work

return $postData;// this will not work

you have to echo the data in ajax call

echo $postData;

You are sending a POST data information as if it were GET...

change your data to:

data: {
   'insertPost':1,
   'title':"'+title+'",
   'content':"'+content+'"
}

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