0

i need to send a request to my controler, but ajax throwing and error. I can't seem to figure out why. Here is the code

HTTPS is before defined constant, which i cannot write here!

        $("input").on("click", function(){
         var elem = $(this).attr("id");
         var execute;

         if( $(elem).prop('checked') == true ){
             execute = '+';
         } else {
             execute = '-';
         }
            $.ajax({
                type:'POST',       
                url: HTTPS+'/path/to/controller/ctlAccess.php?do='+execute,
                data: {id:elem},
                success: function(data) {
                            console.log(data);
                        },
                error: function(){
                            console.log("An error occurred: " + status + "nError: " + error); //AND ERROR: An error occurred: [object Object]nError: error
                        }
            });
        });
    });

And here is the controller:

    $SysData = new SysTables;

if ($_GET["do"] == "-") {
    $userId = preg_match('/[0-9]*(?=p)/', $_POST['id']);
    $pageId = preg_match('/(?<=p)[0-9]*/', $_POST['id']);
    $result = $SysData->deleteAccess($userId, $pageId);

$data = "ACCESS FOR ". $_POST['id']." DELETED";
    echo $data;
}

I think that requests do not even get to controler! I don't actually know hwere is the error. But path to file is right, and part where elem gets ir value works too, so i dont get where is the error!? Help please, thx

4

2 回答 2

1

您可能应该dataType在 AJAX 调用中指定期望值,如下所示:

dataType: 'HTML'

我还建议切换到所有 POST 数据

所以这是整个 AJAX 的事情(将后端的控制器更改为使用$_POST['do']):

$.ajax({
    type:'POST',       
    url: HTTPS+'/path/to/controller/ctlAccess.php',
    data: {
               'id':elem,
               'do':execute // do might be a reserved word so just encase it in quotes to force it as a string
          },
    dataType: 'HTML',
    success: function(data) {
        console.log(data);
    },
    error: function(jqXHR, textStatus) {
        alert( "Request failed: " + textStatus );
    }
});

我想知道究竟HTTPS是什么。只要您不尝试提出跨域请求,那么您就真的不需要它。

而且,如果您尝试发出跨域请求,那么这就是它自己的独立野兽。

于 2013-10-10T14:05:09.703 回答
0

您在 URL 的末尾附加了“执行”值。由于“执行”是一个加号,这将被转换为一个空格,并且可能被 jQuery 作为无效 URL 丢弃。您可能会尝试使“执行”的内容更具可读性。

您可能还想稍微修改您的错误处理程序,试试这个:

error: function( jqXHR, textStatus ) {
    alert( "Request failed: " + textStatus );
});
于 2013-10-10T14:00:58.163 回答