MMYW
MMYW

Reputation: 67

Web service receiving null with jQuery post JSON

The web service on http://localhost:57501/api/addDatabase has the following code.

    [System.Web.Mvc.HttpPost]
    public ActionResult Post(addDatabase pNuevaConeccion)
    {
        pNuevaConeccion.insertarMetaData();
        return null;
    }

The Ajax function is on a javascript that creates the JSON from the give values on http://localhost:1161/CreateServer.

$(document).ready(function () {

$("#createServer").click(function (e) {

    e.preventDefault(); //Prevent the normal submission action

    var frm = $("#CreateServerID");
    var dataa = JSON.stringify(frm.serializeJSON());
    console.log(dataa);

    $.ajax({

        type: 'POST',
        url: 'http://localhost:57501/api/addDatabase/',
        contentType: 'application/json; charset=utf-8',
        crossDomain: true,
        //ContentLength: dataa.length,
        data: dataa,
        datatype: 'json',
        error: function (response)
        {
            alert(response.responseText);

        },
        success: function (response)
        {
            alert(response);
            if (response == "Database successfully connected") {
                var pagina = "/CreateServer"
                location.href = pagina


            }
        }
    });

});

}); When I run this code an alert pops up saying "undefined" but if I delete the contentType the alert doesn't show up. The problem is that the variables that the function Post (from the web service) receives are NULL even though I know that the JSON named dataa is not NULL since I did a console.log.

I have seen various examples and pretty much all of them say that I should use a relative URL but the problem is that since there are 2 different domains and when I tried it, it couldn't find the URL since it's not in the same localhost.

Upvotes: 0

Views: 264

Answers (3)

Mahabub Rabbani
Mahabub Rabbani

Reputation: 134

its my old code.(ensure action parameter variable name and post variable name are same) 

$('#ConnectionAddres_ZonesId').change(function () {
            var optionSelected = $(this).find("option:selected");
            var id = { id: optionSelected.val() };

            $.ajax({
                type: "POST",
                url: '@Url.Action("GetParetArea", "Customers")',
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(id),
                dataType: "json",
                success: function (data) {
                    $('#ConnectionAddres_ParentAreaId').empty().append('<option value="">Select parent area</option>');
                    $.each(data, function (index, value) {
                        $('#ConnectionAddres_ParentAreaId').append($('<option />', {
                            value: value.Id,
                            text: value.Area
                        }));
                    });
                },
            });
        });

 public ActionResult GetParetArea(int id)
        {
            var parents="";
            return Json(parents, JsonRequestBehavior.AllowGet);
        }

Upvotes: 0

Jeric Cruz
Jeric Cruz

Reputation: 1909

try to use this code for calling the web method

$.ajax({
        method: "POST",
        contentType: "application/json; charset=utf-8",
        data: dataa,
        url: 'http://localhost:57501/api/addDatabase/',
        success: function (data) {
            console.log(data);              
        },
        error: function (error) {
            console.log(error);  
        }
});

Upvotes: 0

Amir
Amir

Reputation: 145

Web service should return a JSON format instead of null. like below example.

public JsonResult Post()  
    {  
        string output = pNuevaConeccion.insertarMetaData();

        return Json(output, JsonRequestBehavior.AllowGet);  
    }  

Upvotes: 1

Related Questions