JSON object arrives empty to my method

3

I created the following function to send an object of a service through JSON but when the object arrives it has all the empty variables:

var cpDate = new Object();
    cpDate = {
    OllPass: '',
    NewPass: '',
    ConfPass: ''
};

$(document).ready(function () {
    // Guardar Contraseña
    $('#btnAceptarCP').click(function () {
        ChangePass();
    });
});

 function ChangePass() {
    cpDate.OllPass = $('#txtOllPass').val();
    cpDate.NewPass = $('#txtNewPass').val();
    cpDate.ConfPass = $('#txtCPass').val();
}

$.ajax({
    type: "POST",
    url: '../Servicio/Servicio_Prueba.asmx/setContrasena',
    data: JSON.stringify({ solicitud: cpDate }),
    contentType: "application/json",
    async: false,
    dataType: "json",
    success: function (result) {
        alert(result.d);
    },
    error: function (result) {
        //Log the error to the console
    }
});

Service where I expect the object:

public class Servicio_Prueba : System.Web.Services.WebService
{
    [WebMethod(EnableSession = true)]
    public object setContrasena(EntidadPass solicitud)
    {
        bool guardado = true;
        solicitud.setContrasena(solicitud);
        return guardado;
    }
}

EntidadPass :

public class EntidadPass
{
    string OllPass { get; set; }
    string NewPass { get; set; }
    string ConfPass { get; set; }

    public void setContrasena(EntidadPass solicitud)
    {

    }
}

What I want to know is why the variables arrive empty.

    
asked by Oscar Diaz 09.03.2017 в 13:39
source

6 answers

3

Change your class EntidadPass , your attributes can not be serialized, because they are not public.

public class EntidadPass
{
    public string OllPass { get; set; }
    public string NewPass { get; set; }
    public string ConfPass { get; set; }

    public void setContrasena(EntidadPass solicitud)
    {
    }
}

example working:

Json arrives empty to webservice

    
answered by 09.03.2017 в 19:05
1

Complementing the correct answer from @Said, because the properties were private and not public were not contemplated in the serialization to the request object.

Another problem is when sending the parameter by ajax , consider:

var parametro = {
    solicitud: {
        OllPass: $('#txtOllPass').val(), 
        NewPass: $('#txtNewPass').val(),
        ConfPass: $('#txtCPass').val()
    }
};

$.ajax({
    type: "POST",
    url: '<%= ResolveClientUrl("~/Servicio/Servicio_Prueba.asmx/setContrasena")%>',
    data: JSON.stringify(parametro),
    contentType: "application/json",
    async: false,
    dataType: "json",
    success: function (result) {
        alert(result.d);
    },
    error: function (result) {
        //Log the error to the console
    }
});
    
answered by 09.03.2017 в 23:31
0

Can you try this way?

    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
    public static object setContrasena(EntidadPass solicitud)
    {

    }
    
answered by 09.03.2017 в 14:17
0

Try modifying your Ajax:

$.ajax({
    type: "POST",
    url: '../Servicio/Servicio_Prueba.asmx/setContrasena',
    data: {solicitud: JSON.stringify(cpDate)}, // Esta línea es la que hay que modificar
    contentType: "application/json",
    async: false,
    dataType: "json",
    success: function (result) {
        alert(result.d);
    },
    error: function (result) {
        //Log the error to the console
    }
});

Explanation:

The way you send your data is wrong:

data: JSON.stringify({ solicitud: cpDate })

you are sending this: "{'solicitud': {'OllPass': '', 'NewPass': '', 'ConfPass': ''}}" , however what you should send is something like: solicitud: "{'OllPass': '', 'NewPass': '', 'ConfPass': ''}" , note that the name of the object you send is outside the JSON and in your code is inside; what you are sending is only a string without parameter name, therefore, as you should send it is:

data: {solicitud: JSON.stringify(cpDate)}
    
answered by 09.03.2017 в 18:20
-1

It seems to me that the theme is when generating the json on the JavaScript side. Try with JSON.stringify(cpDate) for your object to serialize properly:

var cpDate = {};

cpDate.OllPass = '123';
cpDate.NewPass = '456';
cpDate.ConfPass = '789';
console.log(JSON.stringify(cpDate))

As you'll see, the solicitud: part in the ajax call is not required. Finally, your ajax request would be as follows:

$.ajax({
    type: "POST",
    url: '../Servicio/Servicio_Prueba.asmx/setContrasena',
    data: JSON.stringify(cpDate),
    contentType: "application/json",
    async: false,
    dataType: "json",
    success: function (result) {
        alert(result.d);
    },
    error: function (result) {
        //Log the error to the console
    }
});
    
answered by 09.03.2017 в 16:59
-3

Only define your method as static and test

Update:

How you answered Gabriel previously should work for you, if this does not work check the file RouteConfig.cs in the App_Start folder and modify or comment on the following line:

//settings.AutoRedirectMode = RedirectMode.Permanent;
    
answered by 09.03.2017 в 13:45