1

I write a script to make an ajax call here is my code

function ajax_post(obj) {
    obj = '#'+ obj; 

    var formData = $(obj).serializeArray();    

    $.ajax({
        url: '__core/info.php',
        type:'get',
        dataType: 'json',
        data: formData,
        success: function(resp){
            alert(resp);
        }
    })
}

and here is my info.php

$last_Res = theme::get_last_themes_desk(); //$last_Res is an array 
echo(json_encode($last_Res));

but when alert it shows return object object ..... what should i do if datatype is json should i convert it to another format ? $last_Res is an array

10
  • 6
    alert's can not show objects, try using the console (F12) -> console.log(resp) Commented Nov 16, 2012 at 14:48
  • how can i alert my $last_res[0] can i? Commented Nov 16, 2012 at 14:50
  • in console.log its ok but i want to alert a part 1 of my array but how ? Commented Nov 16, 2012 at 14:51
  • 2
    @adeneo is right, the alert will display [Object object] when the variable you pass is (you guessed it) an object. You are getting an object because jQuery is parsing the JSON into an object for you. Just use the object. Commented Nov 16, 2012 at 14:52
  • 1
    can you show us the true content of $last_Res ? Commented Nov 16, 2012 at 14:52

3 Answers 3

3

In response to your comment (showing the response data, which is an array, containing a single object):

//resp = [{"id":"2","name":"babak_theme","sh_describ":"support css3 and ie 9 ","rate":"3","time":"2"}];
resp = resp[0];
alert('id => ' + resp.id + ', Name => ' + resp.name);//etc...

Will serve you just fine...

$last_Res is an associative array, most likely. JS doesn't have assoc arrays, but converts these to objects/ object literals:

//php:
$foo = array('foo' => 'bar');
//in JS:
var foo = {foo: 'bar'};
alert(foo);//alerts [object Object]
console.log(foo);//shows you what properties/prototypes/methods the object has

That's all there is too it. To access the data:

for (var prop in resp)
{//for "assoc arrays"
    if (resp.hasOwnProperty(prop))
    {
        alert(prop + ' => '+resp[prop]);
    }
}
for (var i=0;i<resp.length;i++)
{//for regular arrays
    alert(i + ' => ' + resp[i])'
}
Sign up to request clarification or add additional context in comments.

9 Comments

or just use jQuery.each().
my firedn it returns 0=>[object object] again
@user1718141, of course: it's still an object. That's why I proceeded to add an example that alerts resp.id <-- the .id part accesses a single property of an object. I'm sorry, but if that still throws you, MDN has a good crash course on Objects
@jpo: Why use a jQ method on a native JS type, that's just not necessary. Sure premature optimization is the root of all evil, but at least consider the pro's and con's of what you do on a basic level
@Elias Van Ootegem: Because he has already included the jQuery library and it iterates over both (literal objects and arrays) just fine. The performance loss is minimal.
|
1

In your info.php, you should set the Content-Type header to application/json to indicate what you are returning:

header('Content-Type: application/json');

Comments

0

you haven't posted the json format. usually you can access resp. values like this:

if the json format is:

data['index']['subindex1'] = 'value1'
data['index']['subindex2'] = 'value2'

you can

alert(resp.index.subindex1);

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.