Submit form using jquery

一世执手 提交于 2020-01-06 12:49:20

问题


I'm trying to submit a form using JQuery. My problem comes from the fact that the front end (html+js/jquery) and the back-end is not on the same site, but it does support JSONP.

The form contains a file input field, so I would be submitting Multi-part form data. How would you resolve this?


回答1:


your form action would point to the site controlling the post.

<form id="theForm" action="http://someurltoaformsubmitfunction" method="post">

then you can call $('#theForm').submit();




回答2:


If all you want to do is submit the form an go to the external site (ie as if you pressed a submit button on a tranditional web form), you can just trigger the form's submit method using Javascript; it doesn't matter where the form posts to.

document.myform.submit();

However, if you want to post cross-domain using an AJAX-type method, you'll have a harder time of it. The answer lies in using JSONP rather than JSON in your JQuery AJAX requests.

See the JQuery Ajax documentation for more details.




回答3:


EDIT: Do not try this, it will not work for cross domain posts. My fault for not reading the question carefully enough.

Does it have to be a form submit? If not, you could simply do a jQuery ajax call that posts json to it similar to this:

$.ajax({
    url: 'yourUrl.htm',
    data: 'somethingYouWantToSendToQueryString',
    datatype: 'json',
    success: function (data) {
        //Do something with the data
    }
});


来源:https://stackoverflow.com/questions/3979804/submit-form-using-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!