简体   繁体   中英

jQuery AJAX response not displaying

I have a simple AJAX call, and a php script that echos. For some reason, my AJAX is not logging the response. The script executes, but it writes onto a different page. I think there's a problem with my Javascript, but the same thing works on other files perfectly fine. Any insight would be great.

$.ajax({
        url: url,
        type: type,
        data: data,
        success: function(response){
            console.log(response);
        }
    });

This is the PHP echo that I tested with. The data variable in the AJAX call is simply a set of input text values populated elsewhere.

<?php

echo "Hello World";

?>

I guess it would be more helpful to post the full PHP script.

<?php
include 'dbcon.php';

if(isset($_POST['text_login_username'], $_POST['text_login_password'])) {

    $dbcon=getConnection();
    $loginUsername=$_POST['text_login_username'];
    $loginPassword=$_POST['text_login_password'];

    /*SQL INJECTION PREVENTION*/
    $loginUsername=stripslashes($loginUsername);
    $loginPassword=stripslashes($loginPassword);
    $loginUsername = mysqli_real_escape_string($dbcon,$loginUsername);
    $loginPassword = mysqli_real_escape_string($dbcon,$loginPassword);

    /*CHECK IF THERE IS A ROW WITH THIS USERNAME AND PASSWORD*/
    $sqlRequest = "SELECT * FROM userData WHERE userName='$loginUsername' and userPassword='$loginPassword'";
    $sqlRequestResult=mysqli_query($dbcon,$sqlRequest);
    $verifySqlRequest=mysqli_num_rows($sqlRequestResult); //count number of rows satisfying the request

    if($verifySqlRequest==1){
        setcookie("user",$loginUsername);
        header("location:http://localhost/mySite/dashboard.html");
        exit;   
    }
    else{
        echo "[ext.W]: Wrong user name or password..."; //THIS DOESN'T GET LOGGED.
    }

}


?>

Well, example seems to be ok.
At first check if given URL at AJAX options responses correctly when you type it in browser.
Remember, if you want PHP to respond correctly, request should go through the Apache (or another server-side server). If browser gives you 404 (Page not found) check if Apache server is configured correctly - the URL you request should appear in Vhosts settings.

THe problem is you are not specifying POST in your ajax request:

$.ajax({
    url: url,
    type: type,
    method : "POST", // this needs to be there! Default is GET
    data: data,
    success: function(response){
        console.log(response);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM