Ajax returning [Object Object]

Asked

Viewed 1,848 times

3

HTML

index.html

<!DOCTYPE html>
<html>
	<head>

		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
		<script src="script.js"></script>
		<link href="css/refreshform.css" rel="stylesheet">

	</head>
<body>

	<div id="mainform">
		<h2>Submit Form Using AJAX and jQuery</h2> 
		<div id="form">
			<h3>Fill Your Information !</h3>
			<div>
				<label>Email :</label>
				<input id="email" type="text">
				<label>Password :</label>
				<input id="password" type="password">
				<input id="submit" type="button" value="Submit">
			</div>
		</div>
	</div>

</body>
</html>

Javascript

script js.

$(document).ready(function(){
	$("#submit").click(function(){
		var name 		= $("#name").val();
		var email 		= $("#email").val();
		var password 	= $("#password").val();
		var contact 	= $("#contact").val();

		var dataString = '&email1=' + email + '&password1=' + password;
			if(email == '' || password == '' )
			{
				alert("Por favor preencha todos campos");
			}
			else
			{
				$.ajax({
					type: "POST",
					url: "http://192.168.0.10/apps/ajaxsubmit.php",
					data: dataString,
					datatype: 'json',
						success: function(data){
							$('#result').html(data.status +':' + data.message);   
            				$("#result").addClass('msg_notice');
            				$("#result").fadeIn(1500);  
						}
				});
			}
		return false;
	});
});

PHP

ajaxsubmit.php

<?php
header('Content-Type: application/json');
$connection = mysql_connect("localhost", "root", ""); 

$db = mysql_select_db("mydba", $connection); 

$email2     =   $_POST['email1'];
$password2  =   $_POST['password1'];

$query = mysql_query("SELECT * FROM form_element WHERE email = '" .mysql_real_escape_string($email2). "' AND password = '" .mysql_real_escape_string($password2). "' ");

$num_row = mysql_num_rows($query);

if($num_row>0)
{

     echo json_encode(array('status' => 'success','message'=> 'The group has been removed'));

}else{

     echo json_encode(array('status' => 'success','message'=> 'The group has been removed'));
}

mysql_close($connection); 

What is returning:

inserir a descrição da imagem aqui

  • 1

    the error returns after the Submit event?

  • yes after I send it to the server it returns this message, but I would like it to return the json message

  • Alert does not resolve objects, it tries to give console. :)

2 answers

0

Transform into a JSON:

alert(JSON.stringify(data));

console.log(JSON.stringify(data));

0


This has a quick response, the Alert that was made does not solve the object to string, for that has to give console.log(); to view the received message in object that it will be something like this:

[status: 'success', message: 'qll coisa']

to access one of the elements this is enough:

data.message
  • Success: Function(data){ console.log(data.message); } but keeps returning Object

Browser other questions tagged

You are not signed in. Login or sign up in order to post.