1

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!

skndstry
  • 678
  • 1
  • 7
  • 21
  • 1
    Your code is vulnerable to sql injections - use http://php.net/manual/de/function.mysql-real-escape-string.php – jantimon Nov 11 '10 at 14:28
  • What is the error you get? Also, `alert("Error: " +textStatus)` is missing a semi-colon (*;*) at the end. – stealthyninja Nov 11 '10 at 14:30
  • @Ghommey: I know, I trimmed the code to the relevant part. – skndstry Nov 11 '10 at 14:33
  • @stealthyninja: Thanks for the tip with the semi-colon. However, it didn't change anything. I don't get any other error than the one that I display using alert(). – skndstry Nov 11 '10 at 14:35
  • You have to use the `xhr.responseText` instead of textStatus http://stackoverflow.com/questions/1637019/how-to-get-the-jquery-ajax-error-response-text – jantimon Nov 11 '10 at 14:35
  • Do you mean `xhr.responseText`? If so, it's emtpy. – skndstry Nov 11 '10 at 14:38
  • Not sure if you've tried this, but in Firebug's Net tab, right-click on the relevant link and try both `Open in new tab` and `Open response in new tab`. The former will make a new request, exactly as the AJAX did (same post data and all), the latter will simply copy the response to a new tab. One of these might give a bit of insight. – Tarka Nov 11 '10 at 15:00

6 Answers6

2

Does the data get inserted to mysql despite the error? If so, can you put echo on your addclpermission.php file to return 'success' and/or 'fail' for mysql_query()? How about stripping this php file to just echo "hello"???

jan
  • 105
  • 2
  • 10
  • Thanks for your input! The data doesn't get inserted. And I tried just echoing "hello", but firebug still shows Status: aborted, and the error function gets called. – skndstry Nov 11 '10 at 14:48
  • are you under linux box ? or IIS? are you sending and requesting data on the same servers? – jan Nov 11 '10 at 14:57
  • I don't know what kind of server I'm working on, but the data is on the same server. – skndstry Nov 11 '10 at 15:07
  • you might want to check the directory permissions from your server particularly with the directory where the post file is called including where the include files are located and ensure there were no changes made to these permissions... – jan Nov 11 '10 at 15:15
  • Thanks for your comment! I believe the problem is elsewhere, please look at the second edit above for more information. – skndstry Nov 11 '10 at 15:21
  • why don't you try including a complete event to know which error, if this is a timeout error or what.. can you try error:function(){..},complete(XMLHttpRequest, textStatus){ alert(textStatus);} and remove the other alert notice on error? – jan Nov 11 '10 at 15:42
  • have you also tried running a stripped file just to test jquery functionality for your server? maybe something else in your code is aborting the ajax process? and check firebug on what line of code gets performed just before this ajax call? – jan Nov 12 '10 at 18:25
1

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;
Josh Ronk
  • 36
  • 3
1

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).

Nathan MacInnes
  • 11,033
  • 4
  • 35
  • 50
  • Thanks for your answer. Requesting the script works perfectly. I tried echoing "hello", which works on its own, but not via ajax. Same applies for no content at all or just html content. – skndstry Nov 11 '10 at 14:56
  • So does the ajax request work if you don't put any PHP content in the php file? Its a process of elimination to work out what's causing the problem. – Nathan MacInnes Nov 11 '10 at 14:57
1

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.

stealthyninja
  • 10,343
  • 11
  • 51
  • 59
  • Thanks, it the request is still aborted. The query however wouldn't work because of foreign keys, but I will try it using values that won't cause a mysql error. – skndstry Nov 11 '10 at 15:06
1

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
}
jxpx777
  • 3,632
  • 4
  • 27
  • 43
0

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 ?

pb2q
  • 58,613
  • 19
  • 146
  • 147