MVC newbie
MVC newbie

Reputation: 599

MVC ajax return string as [object] object

This is my ajax code which submit a formdata with a file. if i remove always my custom string "has" file which will works and return "1234567".i am expecting to return has "has file 1234567" but always throw [object] object

 $( document ).ready(function() {
     $('#scan').change(function (e) {
         debugger

         var element = this;
         var formData = new FormData();
         var totalFiles = document.getElementById("scan").files.length;

         var file = document.getElementById("scan").files[0];
         formData.append("scan", file);
         $.ajax({
             url: '@Url.Action("scancode", "Products")',
             type: "POST",
             dataType: "json",
             data: formData,
             processData: false,
             contentType: false,
             success: function (data) {

                 $('#barcode').val(data);                   
             },
             error: function (err) {

                 document.getElementById('emsg').innerHTML = err;
             }
         });
     });
});

Controller

 public string scancode(HttpPostedFileBase scan) {
        var str = "";
        if (scan !=null)
        {
            str = "has file";
        }

        try
        {


        IBarcodeReader reader = new BarcodeReader();
        // load a bitmap
        var barcodeBitmap = (Bitmap)Bitmap.FromStream(scan.InputStream);
        // detect and decode the barcode inside the bitmap
        var result = reader.Decode(barcodeBitmap);
        // do something with the result
        if (result != null)
        {
            str =str+ result.Text;
        }
        }
        catch (Exception ex)
        {

            str = ex.Message;
        }
        return str;
    }

Upvotes: 1

Views: 6423

Answers (2)

ViVi
ViVi

Reputation: 4474

You have to always return a JsonResult from a controller to the ajax query. Simply convert the string to JsonResult by using Json(stringvalue);

Your code will become :

public JsonResult scancode(HttpPostedFileBase scan) 
{
    var str = "";
    if (scan !=null)
    {   
        str = "has file";
    }
    try
    {
        IBarcodeReader reader = new BarcodeReader();
        // load a bitmap
        var barcodeBitmap = (Bitmap)Bitmap.FromStream(scan.InputStream);
        // detect and decode the barcode inside the bitmap
        var result = reader.Decode(barcodeBitmap);
        // do something with the result
        if (result != null)
        {
            str =str+ result.Text;
        }
    }
    catch (Exception ex)
    {
        str = ex.Message;
    }
    return Json(str);
}

Upvotes: 5

Kapobajza
Kapobajza

Reputation: 2459

You cannot actually return a string from an ajax call. Return a JSON object instead. Change the return type to JsonResult:

public JsonResult scancode(HttpPostedFileBase scan) 

And instead of return str; return the Json:

return Json(new { someString = str }); 

Finally your ajax call should look something like this:

success: function (data) { $('#barcode').val(data.someString); }

Upvotes: 1

Related Questions