0

I have a JS function:

$(document).on('click', '#submitForm', function (e) {
var fileName = $('#fileName').val();
$.ajax({
    type: 'POST',
    url: '/Calculation/FileExist',
    data: { 'fileName': fileName },
    dataType: 'bool',
    success: function (result) {
        if (result.returnvalue) {
            e.preventDefault();
            alert(result.returnvalue);
            alert("The filename already exists. Please choose another one");
        }
        else {
            alert("The file doesn't exist");
        }
    }
});
});

My action:

    public ActionResult FileExist(string fileName)
    {
        bool result = true;
        string path = Server.MapPath(TempPath) + fileName + ".xlsx"; //Path for the file
        string[] Files = Directory.GetFiles(Server.MapPath(TempPath));
        for (int i = 0; i < Files.Length; i++)
        {
            if (path == Files[i])
            {
                //The filename already exists
                result = false;
            }
        }
        return Json(new { returnvalue = result });
    }

What am I doing wrong here? I'm trying to get the bool value from FileExist method, and if it's true stop the form from submitting (e.preventDefault)

3
  • what's happening? open browser developer console, do you get any response? are there any js errors? please provide more info. Commented May 12, 2014 at 10:45
  • Just change your dataType or delete the property completely and then compare the result with "true" or e.g. Commented May 12, 2014 at 10:48
  • ajax is asynchronous call, so even before getting boolean from server your page is submitting. so set async:false in jquery ajax option to behave like synchronous call Commented May 12, 2014 at 10:52

6 Answers 6

2

There is no dataType: 'bool'. Please use dataType:'json' dataType:'text' to send the boolean values

In your case, it should be dataType:'json'

$.ajax({
    type: 'POST',
    url: '/Calculation/FileExist',
    data: { 'fileName': fileName },
    dataType: 'json',
    success: function (result) {
        if (result.returnvalue) {
            e.preventDefault();
            alert(result.returnvalue);
            alert("The filename already exists. Please choose another one");
        }
        else {
            alert("The file doesn't exist");
        }
    }
});

Then

[HttpPost]
public ActionResult FileExist(string fileName)
{
}
Sign up to request clarification or add additional context in comments.

Comments

1

First, specify dataType: 'json' in your jquery ajax request:

$.ajax({
    // <...>
    dataType: 'json'
    // <...>
});

If you would like to use HTTP GET:

public ActionResult FileExist(string fileName) 
{
    // <...>
    return Json(model, JsonRequestBehavior.AllowGet);
}

You can use HTTP POST method:

[HttpPost] // Add this attribute.
public ActionResult FileExist(string fileName) 
{ 
    // <...>
    return Json(model);
}

1 Comment

OP uses POST request. It it not necessary to use JsonRequestBehavior.AllowGet for POST until, if you are going to use it for GET
1

On top of your controller method, you have to put this annotation:

[HttpPost]

Comments

1

Add [HttpPost] to your controller,set dataType:'json' and set async:false in jquery ajax why do you need POST method. Just use GET method and add JsonRequestBehavior.AllowGet in your controller

Comments

0

put datatype asjson not bool ans also add [HttpPost] attribute on your action or other way is to put type:'GET'

Comments

0

The available data types in a service call are:

  1. xml
  2. html
  3. script
  4. json
  5. jsonp

As per your code:

change the dataType to json as you are returning json from the server.

And by default the ActionResult will be using GET while in your ajax call you have specified POST.

So include [HttpPost] at the top of the method.

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.