skndstry
skndstry

Reputation: 671

JQuery ajax error

Ok, so my Ajax call looks like this:

var poststring = "id_Client=" + id_client + "&id_File=" + id_file;
        alert(poststring);
        $.ajax({
            type: "POST",
            url: "addclpermission.php",
            data: poststring,
            error: function(xhr, textStatus, errorThrown){
                alert("Error: " +textStatus)
            }
        });

Everything works fine until the $.ajax(). If I use alert(poststring) the output looks like this:

id_Client=7&id_File=32

Using firebug, I found out that the url "addclpermission.php" is actually requested, but then 'aborted'. The path is correct though, if I copy the url out of firebug and call it directly, no error is displayed. The alert in the 'error' option returns "Error: error"

The file addclpermission.php:

<?php
require_once("../allgemein/includes/dbconnect.php");
$id_File = $_POST['id_File'];
$id_Client = $_POST['id_Client'];
$sql = "INSERT INTO permission (id_File,id_Client) VALUES (".$id_File.",".$id_Client.")";
mysql_query($sql);
?>

I'm pretty sure this code once worked and that I haven't changed that much.

Any ideas?

Thanks!

Edit: I don't think that the error is in the php script, I have multiple ajax calls to several php scripts, but all of them fail the same way.

Edit 2: Now it works! Well, at least half of it. The request is still aborted, but the data gets inserted in the database. But as I said, this isn't the only ajax call and the others still aren't working, and this one is aborted. So I'd really like to know what caused this error and how I can fix it for good. Thanks!

Upvotes: 0

Views: 5256

Answers (5)

Ercan ILIK
Ercan ILIK

Reputation: 1

problem is a --> require_once

require_once("../allgemein/includes/dbconnect.php");

remove this line in a php and write all code here

but I don't know why ?

Upvotes: 0

Josh Ronk
Josh Ronk

Reputation: 36

I am pretty sure your problem is that you are not returning an expected dataType to the .ajax call, if you explicity set the datatype (json or text for example):

$.ajax({
        type: "POST",
        url: "addclpermission.php",
        data: poststring,
        dataType: "json",
        error: function(xhr, textStatus, errorThrown){
            alert("Error: " +textStatus)
        }
    });

Then just echo out the expected datatype, just so the server responds, then ajax will know the request was successful.

<?php
// if your dataType is json
echo json_encode(true);

// if your dataType is text
echo ' ';

// exit so the server can return the request
exit;

Upvotes: 1

jxpx777
jxpx777

Reputation: 3641

When I do jQuery Ajax, I set the data as a Javascript object that jQuery then serializes. Do you have better luck if you provide data: property as an object like this:

data: {
    id_Client: id_client,
    id_File: id_file
}

Upvotes: 1

stealthyninja
stealthyninja

Reputation: 10371

I think the error could be in dbonnect.php or addclpermission.php. Save this in addclpermission.php (make a backup of your current file) and browse to it directly:

<?php
require_once("../allgemein/includes/dbconnect.php");
$id_File = 1;
$id_Client = 1;
$sql = "INSERT INTO permission (id_File,id_Client) VALUES (".$id_File.",".$id_Client.")";
mysql_query($sql);
?>

Please let us know if it works or if you get an error.

Upvotes: 1

Nathan
Nathan

Reputation: 11149

First, I would try just requesting addclpermission.php in the browser and see what happens.

Then, if that works, what if you just make addclpermission.php contain some text, no PHP content at all. Then for each stage that works, gradually add content (so first the include, for example).

Upvotes: 1

Related Questions