Post with no fronend

0

I'm trying to post a form information but I'm not really sure how to do it.

This is my C # form:

   @using (Html.BeginForm("Enviar", "Mensagem", FormMethod.Post, new { enctype = "multipart/form-data", @class = "", @id = "formNovaMsg" }))
                {
                    <div class="">

                        <div style="display: none;">
                            <input id="LojaId" name="LojaId" value="@Model.LojaId" />
                        </div>
                        <div style="display: none;">
                            <input id="ClienteId" name="ClienteId" value="@Model.Mensagens[0].ClienteId" />
                        </div>


                        <!-- /.box-body -->
                        <div class="box-footer">

                            <div class="input-group">
                                <input type="text" id="Conteudo" name="Conteudo" placeholder="Escreva sua mensagem ..." class="form-control">
                                <span class="input-group-btn">
                                    <button type="submit" id="btnSubmit" class="btn btn-warning btn-flat">Enviar</button>
                                </span>
                            </div>
                        </div>


                        <!-- /.box-footer-->
                    </div>
                }

In order to give the post I followed an example that resulted in the following function:

    $("#formNovaMsg").ajaxForm({
    type: "post",
    beforeSubmit: function (arr, $form, options) {
        loading();
    },
    success: function (data) {
        if(!data.Erro){
            $("#conversa").append(data.Html);
            $("#formNovaMsg")[0].reset();
            $(".msgsis").fadeOut(300);
            var elem = document.getElementById('conversa');
            elem.scrollTop = elem.scrollHeight;
        }
    }
});

This view is a kind of chat the idea and he send the message and give the direct post.

When I'm trying this way I'm trying the error Uncaught TypeError: $(...).ajax is not a function I checked Jquery and are not conflicting (I have only 1, 1.9.1).

    
asked by anonymous 02.09.2016 / 18:46

1 answer

1

I suggest you do not give a submit, if it is a "real-time chat", do via javascript itself. For example: change to not give submit in your html

<button type="button" id="btnSubmit" class="btn btn-warning btn-flat">Enviar</button>

and in your javascript

 $('#btnConcluir').click(function () {
    Teste();
 }

function Teste(){
    var postForm = {
        'exemplo': $('#Conteudo').val() //pega o valor no campo digitado pelo usuário
    };
   // lembrando que 'exemplo' será o nome da variavel recebida como parametro, no C#

    $.ajax({
        url: "/WebService/Exemplo/Chat", //altere para o caminho que você vai querer mandar o post
        type: "post", //tipo post
        data: postForm, //os dados
        success: function (response) {
              alert(response["Mensagem"])
              //se deu certo
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //se deu erro
            console.log(textStatus, errorThrown);
        }
    });
}

C # code

[HttpPost]
public void Chat(String exemplo)
{
    var resultado = new
    {
         Erro = false,
         Mensagem = exemplo;
    };

    return Json(resultado, JsonRequestBehavior.DenyGet);
}
    
02.09.2016 / 19:00