Pergunta

Olá, estou tentando salvar dados usando serviços da Web, JQuery e JSON. De algum exemplo na web, recebi um código semelhante, mas estou usando uma string em vez de int:

Html:

<div id="dialogL" title="Create a new Layer">
    <p>Enter Name:</p>
    <input id="txtValue1" type="text" size="40" />
    <p>Enter Description:</p>
    <textarea id="txtValue2" rows="4" cols="30"></textarea>
    <br />
    <input type="button" value="Create" onclick="Save()" /> 

JavaScript:

<script type="text/javascript">
    function Save() { 
        $.ajax({
            type: "POST",
            url: "testservice1.asmx/Save",
            data: "{ 'value1': " + $("#txtValue1").val() + ", 'value2': " + $("#txtValue2").val() + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: Success,
            error: Error
        });
    }

    function Success(data, status) {

    }

    function Error(request, status, error) {
        alert("Error: " + request.statusText);
    }
</script>

ASP.NET

    [WebMethod]
    public bool Save(String value1, String value2)
    {
       DoSave();
       return true;
    }

Mas isso não funcionou e me devolveu uma mensagem de erro interna. O que eu fiz errado?

Foi útil?

Solução

Altere sua parte de dados, em vez disso, use

data: "{ value1: '" + $("#txtValue1").val() + "', value2: '" + $("#txtValue2").val() + "'}",
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top