Answer the question
In order to leave comments, you need to log in
How to return response from php code to $.ajax?
I check on the server if there is a given value that ajax passed to the database and I want to return a response, how can I do this?
Here is the code:
$.ajax({
type: "GET",
url: window.location.href + "?id=" + per
}).error(function(jq, status) {
alert(jq.errorText || status);
}).success(function(data, status, jq) {
alert(data.id);
});
<?
require "lib/rb.php";
R::setup( 'mysql:host=localhost;dbname=base','root','');
if ($_GET['id'] != "") {
header('Content-Type: application/json; charset=windows-1251');
echo '{ "id": "' . $_GET['id'] . '" }';
/* if(R::count('users', "email = ?", array($_GET['id'])) > 0 ){
$_GET['ot'] = 1; /* Еще не решил каким способом буду обращаться к бд, тут только пример*/
} */
exit;
}
header('Content-Type: text/html; charset=windows-1251');
?>
Answer the question
In order to leave comments, you need to log in
Ajax in the url is easier location.origin and set Ajax dataType: json. If you find something in the database, then return json:
json_encode(['result' => $answer])
And on the client, already in response response.result
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question