【问题标题】:AJAX POST Methods In HTMLHTML 中的 AJAX POST 方法
【发布时间】:2015-10-13 16:20:02
【问题描述】:

我正在尝试使用二维数组的 Ajax 调用更新服务器数据库,但每次添加更多字段时,它都会忽略第一个字段,而只更新最后一个字段。有人可以帮忙吗?所以在这种情况下,只有 phonework 字段正在更新。

function Update_user(){
//Get the form data
//There are many ways to get this data using jQuery (you can use the class or id also)
  $.ajax({
  jsonp: 'jsoncallback',
  dataType: 'json',
  type: 'POST',
  cache:false,
  beforeSend: function() {$.mobile.loading('show')},
  complete: function() {$.mobile.loading('hide')},
  crossDomain: true,
  url: 'https://testing.vle.getsmarter.co.za/webservice/rest/server.php',
  data: {
      'wstoken': '**************',
      'moodlewsrestformat': 'json',
      'wsfunction': 'core_user_update_users',
      'users[0][id]': '2328',
      'users[0][firstname]': document.getElementById('name').value,
      'users[0][lastname]': document.getElementById('surname').value,
      'users[0][customfields][0][type]': 'stopcity',
      'users[0][customfields][0][value]':document.getElementById('city1').value,
      'users[0][customfields][0][type]': 'postalcode',
      'users[0][customfields][0][value]':document.getElementById('postc').value,
      'users[0][customfields][0][type]': 'province',
      'users[0][customfields][0][value]': document.getElementById('prov').value,
      'users[0][customfields][0][type]': 'stopcountry',
      'users[0][customfields][0][value]': document.getElementById('country2').value,
      'users[0][customfields][0][type]': 'addressline1',
      'users[0][customfields][0][value]': document.getElementById('1').value,
      'users[0][customfields][0][type]': 'addressline2',
      'users[0][customfields][0][value]': document.getElementById('2').value,
      'users[0][customfields][0][type]': 'phonemobile',
      'users[0][customfields][0][value]': $("#mobile").attr('value'),
      'users[0][customfields][0][type]': 'phonework',
      'users[0][customfields][0][value]': $("#work").attr('value'),    
    },
    success: function(data) {
      // enable previous buttons
    $('#enable').css('visibility', 'visible');
    $('#back1').css('visibility', 'visible');
      // disable previous buttons
    $('#save').css('visibility', 'hidden');
    $('#cancel').css('visibility', 'hidden');
  // diable fields
    window.location.reload();
    alert("Profile updated." );
    },
    error: function() {

      alert('Update has failed!');
    }
  });
}

【问题讨论】:

  • 请正确格式化您的代码。这只会让我们远离你的问题。
  • 我很抱歉。这是我的第一篇文章。

标签: javascript ajax json html multidimensional-array


【解决方案1】:

您的代码没有更新第一个字段错误更新最后一个字段,因为您对所有字段使用相同的名称users[0][customfields][0][value]

  'users[0][customfields][0][type]': 'stopcity',
  'users[0][customfields][0][value]':document.getElementById('city1').value,
  'users[0][customfields][0][type]': 'postalcode',
  'users[0][customfields][0][value]':document.getElementById('postc').value,
  'users[0][customfields][0][type]': 'province',
  'users[0][customfields][0][value]': document.getElementById('prov').value,
  'users[0][customfields][0][type]': 'stopcountry',
  'users[0][customfields][0][value]': document.getElementById('country2').value,
  'users[0][customfields][0][type]': 'addressline1',
  'users[0][customfields][0][value]': document.getElementById('1').value,
  'users[0][customfields][0][type]': 'addressline2',
  'users[0][customfields][0][value]': document.getElementById('2').value,
  'users[0][customfields][0][type]': 'phonemobile',
  'users[0][customfields][0][value]': $("#mobile").attr('value'),
  'users[0][customfields][0][type]': 'phonework',
  'users[0][customfields][0][value]': $("#work").attr('value'),   

尝试为您的字段使用其他名称,例如

  'users[0][customfields][0][type]': 'stopcity',
  'users[0][customfields][0][value]':document.getElementById('city1').value,
  'users[0][customfields][1][type]': 'postalcode',
  'users[0][customfields][1][value]':document.getElementById('postc').value,
  'users[0][customfields][2][type]': 'province',
  'users[0][customfields][2][value]': document.getElementById('prov').value,
  'users[0][customfields][3][type]': 'stopcountry',
  'users[0][customfields][3][value]': document.getElementById('country2').value,
  'users[0][customfields][4][type]': 'addressline1',
  'users[0][customfields][4][value]': document.getElementById('1').value,
  'users[0][customfields][5][type]': 'addressline2',
  'users[0][customfields][5][value]': document.getElementById('2').value,
  'users[0][customfields][6][type]': 'phonemobile',
  'users[0][customfields][6][value]': $("#mobile").attr('value'),
  'users[0][customfields][7][type]': 'phonework',
  'users[0][customfields][7][value]': $("#work").attr('value'), 

【讨论】:

    【解决方案2】:

    问题是您将所有类型和值分配给同一个数组元素

    users[0][customfields][0][type]
                           ^
    

    因此,您必须为每个新的类型/值对增加该索引。 或者,您可以使用实际的数组/对象。

    data: {
          'wstoken': '**************',
          'moodlewsrestformat': 'json',
          'wsfunction': 'core_user_update_users',
          'users': [{
              id:'2328',
              firstname: document.getElementById('name').value,
              lastname: document.getElementById('surname').value,
              customfields: [
                  { type:'stopcity', value: document.getElementById('city1').value },
                  { type: 'postalcode', value: document.getElementById('postc').value },
                  { type: 'province', value: document.getElementById('prov').value },
                  { type: 'stopcountry', value: document.getElementById('country2').value },
                  { type: 'addressline1', value: document.getElementById('1').value },
                  { type: 'addressline2', value: document.getElementById('2').value },
                  { type: 'phonemobile', value: $("#mobile").attr('value') },
                  { type: 'phonework', value: $("#work").attr('value') }
              ]
          }]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多