1

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;
    }
7
  • check whether you are getting a 200 OK response for your ajax call Commented Jan 10, 2017 at 2:11
  • 1
    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 }); and in your ajax call: success: function (data) { $('#barcode').val(data.someString); } Commented Jan 10, 2017 at 2:17
  • 1
    is the dataType: "json", correct in this case ? I don't think files can be posted via ajax....can someone correct me. Commented Jan 10, 2017 at 2:17
  • 1
    Since you specify dataType: "json", your method should be public JsonResult scancode(...) with return Json(str); Commented Jan 10, 2017 at 2:18
  • 1
    @AizhongChen, Because you have specified dataType: "json" and your sending back a string with spaces in it which will be interpreted as 3 items (note all it needs to be is return Json(str); and keep $('#barcode').val(data);) Commented Jan 10, 2017 at 2:27

2 Answers 2

5

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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); }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.