405 (метод не разрешен), использование JQuery и Ajax в веб-приложении - PullRequest
0 голосов
/ 12 октября 2018

У меня небольшая проблема с моим проектом, я не могу добавить пользователя в таблицу базы данных с именем user, когда я отлаживаю код, который он возвращает 405 (Method Not Allowed), мои веб-сервисы работают, но когда я нажимаю на кнопку в моем HTMLстраница, я ничего не могу сделать.Извините, если мои комментарии плохие, я новичок в этой технологии.Я использую VS 2017, и я добавил HTTP-протоколы.

Сценарий с HTML-страницы:

function sendUser() {
    $.ajax({
           method: "POST",
           URL: "./WebService.asmx/Subscribe",
           data: { 'username': $("#user").val() }
     });
}

function userOK() {
    alert("data");
}

function userKO() {}

function addEvents() {
    $("#btnSendUser").click(sendUser);
}

$(function () {
       addEvents();
});

WebMethod от веб-службы

    [WebMethod]
    public int Subscribe(string username)
    {

        int result = 0;
        //connection to the db 
        string cnxString = @"Data Source=myServerAdress;Initial Catalog=myDB;User ID=myUser;Password=myPass";
        //open and close the connection after the execution
        using (SqlConnection cnx = new SqlConnection(cnxString))
        {
            try
            {
                cnx.Open();
                //create a command with a query and the parameter typed in a form
                SqlCommand cmd = new SqlCommand($"INSERT INTO [User] (name) VALUES ('{username}')");
                //set the connection 
                cmd.Connection = cnx;
                //return the row affected as a string 
                result = int.Parse(cmd.ExecuteNonQuery().ToString());
            }
            catch (Exception)
            {
                throw new HttpException("error cnx");
            }

        }
        return result;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...