weixin_33670786 2015-11-21 13:32 采纳率: 0%
浏览 10

发送数据ajax

I have a page called post-blog.php, in here I've set-up a blog entry. I have all this data being saved into one variable. Its displaying this data in an array.

var data = title + content + image + datetime + categories;

How can I send this data to another page called publish.php and redirect the user to that page ? I've tried to set up a ajax to do this but its not working. Any suggestions ?

  $.ajax({
        type:   'POST',
        cache:    false,
        url:      'publish.php',
        data:     data,
        success:  function( data ) {

          alert ( data );

        }

      });

      return false;

    });
  • 写回答

2条回答 默认 最新

  • weixin_33698043 2015-11-21 13:37
    关注

    I am assuming all your variables are strings. If they are not, for example the datetime may be an object, change them into a string first.

    Docs say Object must be Key/Value pairs or a string.

    Objects work well for this, try something like:

    var data = {title: title, content: content, image: image, datetime: datetime, categories: categories};
    

    If your data is coming from a form check out jQuery's serialize.

    I've never tried to pass as a string in a POST, but my gut feeling is it would need to be in a format similar to passing the data through the url.

    var data = 'title=' + title + '&content=' + content;
    

    Also keep in mind the data in the success function is not the same as what is being passed to the php page. This is what the php page will return. If you're php page returns nothing your alert will be empty. When I'm testing I like to throw something like echo $_POST['title']; in the php file to see something come back.

    Here is a similar question that might help too.

    评论

报告相同问题?