0

I am not sure why i am getting parse error. I am setting the dataType dynamically on my ajax and when i call it with json as the dataType, i get a parse error.

MVC controller action

    public ProductViewModel UpdateProduct(ProductViewModel product)
    {
        var productContract = Mapper.Map<ProductViewModel, ProductContract>(product);
        var productReturned = _productService.UpdateProduct(productContract);

        if (productReturned.HasValue)
        {
            return Mapper.Map<ProductContract, ProductViewModel>(productReturned.Value);
        }
        return null;
    }

Ajax call

var ajaxCall = function (destinationUrl, dataToPost, ajaxDataType, element, callbackFunction) {
    $.ajax({
        url: destinationUrl,
        data: dataToPost,
        contentType: "application/json",
        dataType: ajaxDataType,
        success: function (data) {
            callbackFunction(data, element);
        },
        error: function (req, status, errorObj) {
            console.log(status);
        }
    });
}

callbackFunction

This is the method that runs on the ajax success, my database is update successfully but i need the data returned to updated the UI. I have trying getting it like data, data.d and data.d[0]

function updateProductRow(data, element) {
    console.log(data.d[0]);
}
floormind
  • 1,868
  • 5
  • 31
  • 85
  • did you check in developer tools the return data? – Karthik M R Apr 24 '16 at 19:05
  • @KarthikMR thats why i'm logging the data returned, its parseerror – floormind Apr 24 '16 at 19:20
  • 1
    That means you are not returning valid json. The return null will definitely cause that. I'm not familiar with the code in the other return case, is that producing json? It doesn't look like it – Wesley Smith Apr 24 '16 at 19:22
  • @DelightedD0D Hey, thanks for replying, the data i am sending is valid, therefore it does not hit the return null part of the code, instead it returns an object. – floormind Apr 24 '16 at 19:50
  • Hmm, Im not sure that you're understanding of `dataType` is correct. Per the docs. "dataType (default: Intelligent Guess (xml, json, script, or html)) Type: String, *The type of data that you're expecting back from the server.*" You are returning an object directly, you likely need to convert it to JSON first. Maybe by using some serialization function but Im not familiar with asp but this one looks about right http://stackoverflow.com/questions/6201529/turn-c-sharp-object-into-a-json-string-in-net-4 – Wesley Smith Apr 24 '16 at 19:59
  • Also be aware that if your server code throws an error/warning and you have error reporting turned on, or your server code produces any other response that will produce an invalid JSON response (at least thats the case with php). – Wesley Smith Apr 24 '16 at 20:06

1 Answers1

0

I figured it out, in the case of ASP.NET MVC application with the controller action, I just had to have an attribute above my method to accept verb which will be a post. And also to convert the return type to a JSON result.

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult UpdateProduct(ProductViewModel product)
    {
        var productContract = Mapper.Map<ProductViewModel, ProductContract>(product);
        var productReturned = _productService.UpdateProduct(productContract);

        if (productReturned.HasValue)
        {
            return Json(Mapper.Map<ProductContract, ProductViewModel>(productReturned.Value));
        }
        return null;
    }
floormind
  • 1,868
  • 5
  • 31
  • 85
  • As an observation, where you have `return null;`, you might consider returning a valid JSON response with a meaningful error message instead of null. Currently, if you ever hit the `return null;` you will get the same parse error you asked about. Having a meaningful error message returned instead is more graceful and will help you provide a better UX overall. Just my two cents though :) – Wesley Smith Apr 24 '16 at 22:20
  • @DelightedD0D Thank you!... i had planned to return something like {description: "an error has happened"}.. do you have an email? – floormind Apr 24 '16 at 22:50