1

I'm new in JQUERY and trying AJAX.I need to access the array so i can put the element on my html table. btw the array comes from my backend.

the elements in the data array are:

[1990, "098765", "094561", "098123", "097612"]

I tried the usual way but it wont work, How to do it right? really need help

my Jquery code:

 $('#trigger').click(function(){
            var send = $('#myselect').val();
            $.ajax({

                data:{
                  sent: send
                },
                type: 'POST',
                url: '/delinquincy'
            })
            .done(function(data){
              console.log(data)
              var q1=data[1]
              var q2=data[2]
              var q3=data[3]
              var q4=data[4]

              $('#q1').html(q1)
              $('#q2').html(q2)
              $('#q3').html(q3)
              $('#q4').html(q4)
            })
          })

This is where I put the elements of the array,

my HTML Table:

<table class="table table-bordered" id="subtable">
              <thead>
                  <tr>
                    <th scope="col">Quarter</th>
                    <th scope="col">O.R.number</th>
                    <th scope="col">Action</th>
                  </tr>
              </thead>
              <tbody>
                <tr>
                  <td>1st Quarter</td>
                    <td id="q1"> </td>
                    <td><button type="button" class="btn btn-primary subbutton" data-toggle="modal" data-target="#submodal" id="pay" data-row-val="" >Pay</button></td>
                </tr>

                <tr>
                  <td>2nd Quarter</td>
                    <td id="q2"> </td>
                    <td><button type="button" class="btn btn-primary subbutton" data-toggle="modal" data-target="#submodal" id="pay" data-row-val="" >Pay</button></td>
                </tr>

                <tr>
                  <td>3rd Quarter</td>
                    <td id="q3"> </td>
                    <td><button type="button" class="btn btn-primary subbutton" data-toggle="modal" data-target="#submodal" id="pay" data-row-val="" >Pay</button></td>
                </tr>

                <tr>
                  <td>4th Quarter</td>
                    <td id="q4"> </td>
                    <td><button type="button" class="btn btn-primary subbutton" data-toggle="modal" data-target="#submodal" id="pay" data-row-val="" >Pay</button></td>
                </tr>

              </tbody>
            </table>
5
  • 1
    What is the problem? You are correctly accessing it Commented Jan 4, 2018 at 7:53
  • What is the result of conolse log? Do you have any errors or whatever? Commented Jan 4, 2018 at 7:56
  • waht shows console.log in your cosnole ? Commented Jan 4, 2018 at 8:01
  • When I console.log(q1) the result is another element but not from the data array Commented Jan 4, 2018 at 8:03
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jan 4, 2018 at 8:29

1 Answer 1

3

Since you have no dataType set on the request, the response will always come as a string, not as a ready to use javascript array. Use

data = JSON.parse(data) 

before

$('#q1').html( data[0] )

or add

dataType: "json",

to your AJAX request.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.