问题描述:

I'm really new to web programming. I'm trying to make a form post and get the callback.

I'm trying to use this plugin here: http://malsup.com/jquery/form/#ajaxSubmit

But when I call the: $("#my_form").ajaxSubmit(options); Nothing happens..

What I have done so far:

I have this form

 <form method="post" id="my_form" action="record.php" enctype="multipart/form-data" >

// stuff inside..

<input type="button" id = "recordDatabase" value="Rcord on Database" />

</form>

And I have this script:

 <script src="http://malsup.github.com/jquery.form.js"></script>

$(document).ready(function()

{

var options =

{

beforeSubmit: showRequest, // pre-submit callback

success: showResponse // post-submit callback

};

$("#recordDatabase").click(function()

{

alert('About to submit: \n\n');

$("#my_form").ajaxSubmit();

alert('submited: \n\n');

return false;

});

});

Finally my two functions are:

function showRequest(formData, jqForm, options)

{

// formData is an array; here we use $.param to convert it to a string to display it

var queryString = $.param(formData);

alert('About to submit: \n\n' + queryString);

return true;

}

function showResponse(responseText, statusText, xhr, $form)

{

alert('status: ' + statusText + '\n\nresponseText: \n' + responseText +

'\n\nThe output div should have already been updated with the responseText.');

}

Im doing exactly like the example on the site(http://malsup.com/jquery/form/#ajaxSubmit), but it doesnt work.

Any idea what's wrong?

网友答案:

I don't think you can hotlink to the jQuery plugin on Git. Try downloading the plugin and saving it as a JS file in your application web root.

网友答案:

It looks like you are not referencing your scripts correctly. According to your comment, you have included your scripts like this:

<script src="ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="localhost/js/jqueryPlugin.js"></script>

These are relative URLs. The browser will request the resources from your site by tacking on those relative URLs to the end of the current directory.

Suppose this page is at http://localhost/myapp/mypage.html. The browser will look for your script files at:

http://localhost/myapp/ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
http://localhost/myapp/localhost/js/jqueryPlugin.js

These URLs probably don't exist. Maybe your browser is smart enough to recognise "ajax.googleapis.com" as a domain name and request the data from the domain, but it's less likely that it will recognize "localhost" as a domain.

Add // to the beginning of the URL. This is a "Schema-relative URL" and will use either http or https depending on what the current page is using. We use these kind of URLs to avoid security prompts warning users that some of the content on the page is not secure. Your scripts would then look like this:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//localhost/js/jqueryPlugin.js"></script>
相关阅读:
Top