Ajax produces [object object]?

I get [object of object] from my ajax response.

$.ajax({ type: "GET", data: "id_1="+id_1+"&id_2="+id_2, url:"ajax/url.php" }).done(function(data){ var left= $(data).find("#left"); $("#left").html(left); alert(left); }); 

In my url, I just have a simple encoding

 if(isset($_GET["id_1"]) && isset($_GET['id_2'])){ $id_1 = $_GET["id_1"]; $id_2 = $_GET['id_2']; $right= $dbh->prepare("SELECT COUNT(*) FROM table WHERE id_1 = ?"); $right->execute(array($id_1)); $left= $dbh->prepare("SELECT COUNT(*) FROM table WHERE id_1 = ? "); $left->execute(array($id_2)); <div id='right'><?php echo $right->fetchColumn();?></div> <div id='left'><?php echo $left->fetchColumn();?></div> } 

When I do all this, it sends back [the object of the object]

Does anyone know why he is doing this?

Thanks!

EDIT:

I added some coding to .done (function ())

+6
source share
5 answers

I solved my problem. The problem is that I needed to wrap my div in another. After that, the find () method will be able to capture these id and return the corresponding objects.

 if(isset($_GET["id_1"]) && isset($_GET['id_2'])){ $id_1 = $_GET["id_1"]; $id_2 = $_GET['id_2']; $right= $dbh->prepare("SELECT COUNT(*) FROM table WHERE id_1 = ?"); $right->execute(array($id_1)); $left= $dbh->prepare("SELECT COUNT(*) FROM table WHERE id_1 = ? "); $left->execute(array($id_2)); <div> // wrapper <div id='right'><?php echo $right->fetchColumn();?></div> <div id='left'><?php echo $left->fetchColumn();?></div> </div> } 
+1
source

data is an object. [object Object] is just the response of the toString() object.

You need to access the data of the object. Try using console.log(data) to check its contents.

It looks like you didn’t specify the code as is from your PHP example. The code you submitted will be a syntax error.

Also check the MIME response type. You can force dataType as html .

+11
source

You are returning an object, not a simple string ... It seems that the "data" will contain the answer from the page "url.php", What are you trying to do with the "data" in your real application? I assume a warning is just a debugging measure?

0
source

Where do you create the JSON response? I assume this is the fetchColumn () method. Double check to make sure you cook with json_encode () correctly ...

What do you get when you drop everything?

 var_dump( $right ); var_dump( $down ); var_dump( $right->fetchColumn() ); var_dump( $down->fetchColumn() ); 
0
source

Try it ..

 $response = ""; $response .="<div id='right'>".$right->fetchColumn()."</div>"; $response .="<div id='down'>".$down->fetchColumn()."</div>"; echo $response;exit; 
0
source

Source: https://habr.com/ru/post/911830/


All Articles