7

I'm using the jquery.form plugin to asynchronously upload documents in an MVC project.

Taking my lead from this previous answer, here's what I have on the page:

<% using(Html.BeginForm("Create", "JobFile", FormMethod.Post, new { id = "uploadForm", enctype = "multipart/form-data" })) %>
    <% { %>
    <%:   Html.ValidationSummary() %>

          <input type="file" id="fileToUpload" />
          <input type="submit" value="Upload file" />
          <input type="text" id="RelatedFileName" />
    <% } %>
    
<script type="text/javascript" src="../../Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="../../Scripts/jquery.form.js"></script>
<script type="text/javascript">
    $(function () {
        $('#uploadForm').ajaxForm(function (result) {
            if (result.errorMessage != '') {
                alert(result.errorMessage);
            } else {
                $('#RelatedFileName').val(result.fileName);
            }
        });
    });
</script>

My problem is that when the page loads I get the following javascript error:

Uncaught TypeError: Object # has no method 'ajaxForm'

This error is found on the line containing

$('#uploadForm').ajaxForm(function (result) {

Can anyone tell me why I'm getting this error?

1
  • Just an FYI, /Scripts/jquery.form.js will work the same (if not more consistently) as ../../Scripts/jquery.form.js - both start at the site root. Commented Jul 15, 2012 at 8:33

5 Answers 5

4

Check that jquery isn't being included in the page twice.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a million.! After breakin my head over it for hours and hours, this was the solution,
1

Here is the method that I am using for MVC3, CodeIgniter and Yii. One important this is that your should specify method="post", enctype and encoding="multipart/form-data"

var submitForm = function(_formId){
var _genMsg = $('#genMsg');
_submitForm = $('form').index($('#'+_formId));
if(subCommonForm(_submitForm, 'main')){
    _genMsg.attr('class', 'information-box round');
    _genMsg.html('Saving...');
    $('#'+_formId).ajaxForm({
        dataType: 'json',
        success: function (data) {
            _genMsg.attr('class', 'information-box round');

            data.code == 0
                ? _genMsg.attr('class', 'confirmation-box round')
                : _genMsg.attr('class', 'error-box round');

            _genMsg.html(data.message);
            _genMsg.fadeIn();
        }
    });

    $('#'+_formId).submit();
}
};

Comments

0

Is 'uploadForm' the id of your <form> object? If it is then you may wan to check in something like FireBug to make sure your plug-in is included on your page properly.

8 Comments

Yes, 'uploadForm' is the ID of my form. The downloaded source is "<form action="/JobFile/Create" enctype="multipart/form-data" id="uploadForm" method="post">". If I use the debug/source tool in Chrome I can see that jquery.form.js IS being downloaded (it's listed on the "Scripts" tab).
IE9 says pretty much the same: the script file IS downloading, and the error that it comes up with is "SCRIPT438: Object doesn't support this property or method".
You may have to change your script then so it gets called on page load: $(document).ready(function () { ...code...}); That way the form will have the function prototyped correctly if the plug-in is installed. Otherwise I would look into your script, because that should work.
I've copied the entire jquery.form plugin script and pasted it into a <script> element on the page. Same situation - error message still appears. So downloading the file isn't the root of the issue, but somehow the script isn't being loaded. I've re-downloaded the file to ensure that I haven't missed a closing brace or anything like that, but I still have the problem.
Is it stored locally? If so it may be a permissions issue.
|
0

The shortcut:

$(function () {

Doesn't work in latest version of jQuery, have to use:

 $(document).ready(function () {

4 Comments

That's helpful to know, but I wouldn't be getting the error on page load if "$(function(){" wasn't working in the version I'm using. Nonetheless, thanks for the tip, and I have now changed it.
This is just plain wrong. I have 1.7.2 and this works without any errors.
At the time of writing it was true of the latest version
Hmm. I didn't notice the date-stamp. If you edit your question I can remove my downvote.
0

check if you have the $

$('#your-form')

and not

('#your-form')

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.