Cris
Cris

Reputation: 21

can't access php with ajax

I've got the Problem that I can't reach my php file with my $.ajax() call. I always get a jqXHR.status of 0. This is my code:

function with $.ajax() call:

function updateClubInfo(text) {
    var data = text;
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '../php/updateInfo.php',
        data: {
            infoText: data,
            action: "update"
        },
        success: function() {
            // do nothing
            alert('success');
        },
        error: function(jqXHR, exception) {
            if (jqXHR.status === 0) {
                alert('Not connect.\n Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
                alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
        }
    });
}

I've build this request similar to others in my project, but this one is the only one that doesn't work. Here the PHP file I want to reach:

php code snippet (updateInfo.php):

<?php
    $action = $_POST['action'];
    $text = $_POST['data'];

    myLog($action);
    myLog($text);

    echo "hello";


    /*
     * Writes entry in Logfile
     */
    function myLog($data) {
        $text = @file_get_contents('log.txt');
        $text = $text . "\n" . $data;
        @file_put_contents('log.txt', $text);
    }   
?>

When I try to reach this PHP file in the URI the echo is outputted. So I think the Problem should be in the ajax call.

Does someone has an Idea what I'm doing wrong? I'm grateful for any help.

Thx alot for your help Cris

Upvotes: 2

Views: 1044

Answers (2)

sdespont
sdespont

Reputation: 14025

You have a copy/paste error in your PHP file. It should be :

$action = $_POST['action'];
$text = $_POST['infoText'];//instead of $_POST['data']

UPDATE

Because your AJAX request asks for JSON data, but you are writing only text in your PHP file. Therefore, the AJAX request interprets the answer as void, then HTTP status = 0

Solution

dataType option is not about the type of data you are sending to the server, but the type or data you are expecting back from the server. Change dataType: 'json', to dataType: 'html', or symply remove it to let JQuery chose the appropriate mode.

Upvotes: 2

user1621675
user1621675

Reputation:

$action = $_POST['action'];
$text = $_POST['data'];

myLog($action);
myLog($text);
$arr[] ='hello';

echo json_encode($arr);


/*
 * Writes entry in Logfile
 */
function myLog($data) {
    $text = @file_get_contents('log.txt');
    $text = $text . "\n" . $data;
    @file_put_contents('log.txt', $text);
}   


json data fetch only in array

Upvotes: 0

Related Questions