Решено: Boa: Bad Request при POST запросе

Модератор: Модераторы разделов

IMB
Сообщения: 2567
ОС: Debian

Решено: Boa: Bad Request при POST запросе

Сообщение IMB »

Доброго дня!
Используется www-сервер Boa 0.94.13.
HTML

Код:

...................... <head>....................... <script type = "text/javascript" src = "js/users.js"></script> </head> <body>.......................... <form src = "" method = "post" id = "add"> <table cols = 2 border = 0> <thead><th colspan = 2>Новый пользователь</th></thead> <tbody> <tr><td>Пользователь</td> <td><input type = "text" name = "user" id = "user"></td> </tr> <tr><td>Пароль</td> <td><input type = "password" name = "password" id = "pwd"></td></tr> <tr><td>Уровень</td> <td><select name = "level" id = "level"> <option value = 0>администратор</option> <option value = 1>оператор</option> <option value = 3 selected>пользователь </option></select></td></tr> <tr><td colspan = 2><input type = "hidden" name = "a" value = "add"></td></tr> <tr><td colspan = 2 class = "button"> <input type = "image" src = "img/b_add_user.png"></td></tr> </tbody> </table> </form> .....................

users.js

Код:

........................ $('#add').submit(function() { alert('add new user') $.ajax({ type: 'POST', url: 'cgi/account.cgi', async: false, dataType: 'json', success: function(data) { if ('ERROR' == data.result) $('#error').html('Ошибка применения параметров (' + data.answer + ')') return false } });

account.cgi

Код:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys_env_type.h> int main(int argc, char *argv[]) { char *query, action[3], user[USER_LEN], passwd[PASSWORD_LEN]; int level, qlen; Acount_t account; if (strcmp(getenv("REQUEST_METHOD"), "POST") != 0) { printf("Content-Type: text/plain\n\n" "{\"result\": \"ERROR\"," "\"answer\": \"Can't get request method or " "request method is't POST!\"}"); return -1; } if (0 == (qlen = atoi(getenv("CONTENT_LENGTH")))) { printf("Content-Type: text/plain\n\n" "{\"result\": \"ERROR\"," "\"answer\": \"Can't get query string length!\"}"); return -1; } fgets(query, qlen, stdin); if (sscanf(query, "&user=%s&pwd=%s&level=%1d&a=%s", user, passwd, &level, action) != 4) { printf("Content-Type: text/plain\n\n" "{\"result\": \"ERROR\"," "\"answer\": \"Can't parse query string!\n%s\"}", query); return -1; } return 0; }

Ответ сервера

Код: Выделить всё

400 Bad Request
Your client has issued a malformed or illegal request.

Firebug (закладка Net/All/Post

Код:

Parametersapplication/x-www-form-urlencoded a add level 3 password t user test x 103 y 11 Source Content-Type: application/x-www-form-urlencoded Content-Length: 45 user=test&password=t&level=3&a=add&x=103&y=11
.
В логах сервера вполне ожидаемое [05/Jan/2000:22:43:05 +0000] Invalid Content-Length [0] on POST!.
При обращении к account.cgi напрямую ожидаемый ответ {"result": "ERROR","answer": "Can't get request method or request method is't POST!"}.
Таким образом проблема не в CGI, до него запрос даже не доходит, что также видно и по ответу сервера.
А в чём проблема?
Спасибо.
Спасибо сказали:
IMB
Сообщения: 2567
ОС: Debian

Re: Решено: Boa: Bad Request при POST запросе

Сообщение IMB »

Wireshark показал, что запрос к CGI уходит с Content-Length:0.
Похоже, что ошибка из за этого, буду разбираться.
Спасибо сказали:
IMB
Сообщения: 2567
ОС: Debian

Re: Решено: Boa: Bad Request при POST запросе

Сообщение IMB »

Решено, прописал а ajax отправляемые данные.
users.js

Код:

................... $('#add').submit(function() { alert('add new user') $.ajax({ type: 'POST', url: 'cgi/account.cgi', async: false, dataType: 'json', data: 'user=' + $('#user').val() + '&pwd=' + $('#pwd').val() + '&level=' + $('#level').val() + '&a=add', success: function(data) { if ('ERROR' == data.result) $('#error').html('Ошибка применения параметров (' + data.answer + ')') return false } });
Спасибо сказали: