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.

    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)