开发者

json submit send a form

In the following can a form 开发者_JAVA技巧be sent through json request which has a file and and a text box in it.I am using django on the server side

<script>
var uploadform=$("#upload_form").val();
function send_data(paramarr,url) {
    $.post(url, paramarr,
    function callbackHandler(ret) {

    },
    "json"
    );
}
</script>

<form id="upload_form">
    <input type="text" name="field_name" >Name:
    <input type="file" name="field_name" >Upload:
</form> 


You won't be able to do file uploads via post() or ajax(). You will either have to use an iframe or use a helper library that will basically just do this automatically for you. Search the jQuery plugin site for an ample number of alternatives.


Have you looked into jquery's serialize() function? This allows you to take a form, serialize it to a string, and pass it as a parameter.

http://api.jquery.com/serialize/

var str = $("#upload_form").serialize();
$.post("test.php", str, function (ret){ 

},"json");

EDIT:

I admittedly did not notice it was a file upload. File uploads can not be done in this way. Check out Uploadify


you can create a form dynamically by fetching form markup from server. However, few issues

  1. If fetched data contains JavaScript, it won't be evaluated (i,e. browser ignores it. You have to manually eval it)
  2. When you submit the loaded form, it submits the entire page (You have to use Ajax to submit only that form, and retain rest of the page as-is.)
  3. If form contains file tags, it wont work (You need to work around this, by creating hidden frame, load form items in to it, submit and remove the hidden form)


Adding to jm_toball's note about not being able to do file uploads via post() or ajax(), you could try using the jQuery forms plugin. I haven't used the plugin for file uploads, but their FAQ says their plugin supports it:

http://jquery.malsup.com/form/#faq

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜