weixin_33737134 2016-06-02 19:46 采纳率: 0%
浏览 21

数组不是由jQuery ajax发送的

I am trying to send an array using the jQuery ajax function, but it isn't working.

Here is my code:

  if (section_name == "first_details_div") {
    var fields_arr = ["f_name", "l_name", "identity_number", "kupat_holim_id", "kupat_holim_insurance_ID", "birth_date", "father_name", "family_status_id"];
    var section_values = new Array();

    for (i = 0; i < fields_arr.length; i++) {

      if (document.getElementById(fields_arr[i]))
        section_values[fields_arr[i]] = document.getElementById(fields_arr[i]).value;

    }
  }

  var array_to_send = $.serialize(section_values);

  $.post("ajax_save_intek_section.php", {
      section_name: section_name,
      section_values: array_to_send
    },
    function(data) {
      alert('here!');
      if (data) {
        alert(data); //"<?=getstring('saved_successfully')?>"
      }

    });

I tried to add this line before (as I saw in other answer):

var array_to_send = $.serialize(section_values);

but it doesn't recognize this function.

Any ideas?

  • 写回答

2条回答 默认 最新

  • weixin_33721427 2016-06-02 19:54
    关注

    Try to use var array_to_send= $(section_values).serialize(); stead. but you also can create an object and serialize it with JSON.sttringify(object);

      var request = {
         section_name: section_name,
         section_values: section_values
       };
    
       $.post("ajax_save_intek_section.php", JSON.stringify(request),
         function(data){
           //rest of your code here
         }
       });
    
    评论

报告相同问题?