PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 8, 2022

[FIXED] How to pass the data as object in jquery ajax

 September 08, 2022     ajax, arrays, javascript     No comments   

Issue

I want to receive the value thought iterator, when I use

$('#someModal').find('input[name=nameProBangHHModal]').val(data[0].TenNhanQuyCach); 

it works as expected.

But when I use an iterator, the return shows [object Object].TenNhanQuyCach - not the data returned.

I try to search for other topic but I'm stuck on this. How to get the data returned in array[data[0]] with the value from the iterator method?

Thank you for your time!

$.ajax({
            url: 'someurl to retrive data',
            type: 'POST',
            data: {id:id},
            success: function(data,status) {
                var tenCot = ['nameProBangHHModal','valueBangHHModal',];
                var tenCotTrongCsdl = ['TenNhanQuyCach','DonViTinh',];
                console.log(tenCotTrongCsdl[0]);
                for (i=0;i <= tenCot.length - 1;i++) {
                    $('#someModal').find('input[name="' + tenCot[i] + '"]').val(data[0]+'.'+tenCotTrongCsdl[i]');
                }

Solution

oh i find out the answer after search topic for array: this one: How can I access and process nested objects, arrays or JSON?. and find out that need to use [] for variable, not the ".". this one work for me:

$('#someModal').find('input[name="' + tenCot[i] + '"]').val(data[0][tenCotTrongCsdl[i]]);


Answered By - anh phan
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing