weixin_33708432 2016-04-02 15:24 采纳率: 0%
浏览 24

jQuery Ajax发布数据

I want to ajax post data which is dependent on my variables. I tried the following but no luck..

var datastring = "name:name,price:price,specialprice:specialprice,desc:desc,descs:descs,cat:cat,pictures:pictures,choiceimg:choiceimg,weight:weight,gtin:gtin,brand:brand,attid:attid";

if($("#color").val()) {
var color = $("#color").val();
datastring = datastring + ",color:color"
}

$.ajax({
  type: 'POST',
  data:{ datastring }, 
  success: function() { },
  error: function(){ },
  url: 'insert.php',
  cache:false
});

This posts as a string

datastring

name:name,price:price,specialprice:specialprice,desc:desc,descs:descs,cat:cat,pictures:pictures,choiceimg
    :choiceimg,weight:weight,gtin:gtin,brand:brand,attid:attid

Any ideas / solutions ?

  • 写回答

2条回答 默认 最新

  • weixin_33695450 2016-04-02 15:37
    关注

    Assuming at the parameters are defined, this is how you need to create the object:

    var name = ....;
    var price = ....;
    .....
    
    var datastring  = {
        name: name,
        price: price,
        specialprice: specialprice,
        'three words property': 'value',
        ......
    };
    

    You can ADD/Change a property to the object (after it was declared) like this

    datastring.myNewProperty = "myString"; // ADD
    datastring.name = "New Name"; // UPDATE
    datastring['three words property'] = "New Value"; // UPDATE
    

    And you pass this to the server:

    $.ajax({
      type: 'POST',
      data: datastring, // Note - I removed the wrapping "{" and "}"
      success: function() { },
      error: function(){ },
      url: 'insert.php',
      cache:false
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度