php - AJAX GET request not working -


i trying make ajax call, it's first time use ajax, code following:

$.get( "validate.php", { 'userinput':'x'}, function(response) {     if( response.status ) alert( "matches found" );     else alert( "no matches" ); }); 

vaidate.php:

<?php $user_input=$_get['userinput']; //print_r($user_input); if(!is_null($user_input)) exit( '{ "status": true }' ); else exit( '{ "status": false }' ); ?> 

if access validate.php, "undefined index" error. proper way this?

the php looks fine once comment out test code:

<?php   $user_input=$_get['userinput'];   //print_r($user_input);   if(!is_null($user_input)) exit( '{ "status": true }' );   else exit( '{ "status": false }' ); ?> 

you need specify expect json, simplest way use getjson

$.getjson( "validate.php", { 'userinput':'x'}, function(response) {     if( response.status ) alert( "matches found" );     else alert( "no matches" ); }); 

other alternative jquery is

$.get( "validate.php", { 'userinput':'x'}, function(response) {     if( response.status ) alert( "matches found" );     else alert( "no matches" ); },"json"); 

or set contenttype header in php:

<?php   $user_input=$_get['userinput'];   //print_r($user_input);   header('content-type: application/json');   if(!is_null($user_input)) exit( '{ "status": true }' );   else exit( '{ "status": false }' ); ?> 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -