javascript - unable to recieve success message from php to Ajax, jQuery || SOLVED -


i working jquery/ajax need php send me success, error message upon status of form receiving. have done bit of googling still unable receive message. using json_encode.

js code:

$(function()   {  $("#loginbtn").click(function(e)   {     var array = [];      var username = $("#login").val();     var passwrd = $("#password").val();     var flag = false;      alert("log in alert");      if(username == '' || passwrd == '')     {         alert("username or password cannot empty");         flag = false;     }     else     {         array.push(username);         array.push(passwrd);          alert(array);         flag = true;     }      if(flag == true)     {         alert("going ajax");         $.ajax({             url:"http://localhost/login.php",             data:             {                 username: array[0],                 passwrd: array[1],             },             type:"post",             datatype:"json",             success:function(suc){                 alert("suc");          alert(suc);                    console.log(suc);             },             error: function(err)             {             alert("err");             alert(err);             console.log(err);         }         });     }     else     {         alert("form error");     }         alert("final check here"); });     }     else     {         alert("form error");     }         alert("final check here"); }); } ); 

php code:

<?php  header('cache-control: no-cache, must-revalidate'); header('expires: mon, 01 jan 2016 00:00:00 gmt');  // json standard mime header. header('content-type: application/json');      $con=mysql_connect("localhost","root", "");      mysql_select_db("monkey",$con);   $name = $_post['username']; $pass = $_post['passwrd'];   $query="insert logincheker(login,password) values('$name','$pass');";      if(mysql_query($query))    {        $response_array['status'] = 'success';         echo json_encode($response_array);    } else {    $response_array['status'] = 'error';     echo json_encode($response_array);  }   ?> 

[ps: both js , php code dummy, php code. not using in server side. ] in advance.

update: db updating informing. each time, hit login button, db gets 1 row of data jquery saying has hit in error function. here object have received.

enter image description here solution:

adding following header solved problem

header("access-control-allow-origin: *"); 

looks issue ajax request, try this:

$.ajax({                     ...     ...     ...     success: function (response) {         console.log(response);     },     error: function (response) {         console.log(response);     } }); 

also, should using prepared statements in pdo db interactions.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -