开发者

JQuery ajax error

开发者 https://www.devze.com 2023-01-24 19:42 出处:网络
Ok, so my Ajax call looks like this: var poststring = \"id_Client=\" + id_client + \"&id_File=\" + id_file;

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 abor开发者_JAVA百科ted, 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!


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"???


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


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.


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
}


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;


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 ?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号