简体   繁体   中英

Remove parameters before sending FORM POST

I have page with one form made in C# on the page there are several HTML elements. On button click I am using jQuery and add hidden fields than I submit form to external domain. Now the issue is, that it is sending all the parameters which I don't need. Is there any way that I can send only parameter that I need. I want to send parameter only in body.

Is there way to clear form parameter before adding parameter

jQuery Code

$("#aspnetForm").attr("action", "www.example.com");
$("#aspnetForm").attr("method", "post");
var params = a.split('?')[1].split('&'); /*custom string with key/value and sperated with & */
$.each(params, function (index) {
    var paramsV = params[index].split('=');
    $("#aspnetForm").append('<input type="hidden" name="' + paramsV[0] + '" value="' + paramsV[1] + '" /> ');
});
$("#aspnetForm").submit();

There more than 50 HTML element are added dynamically in my form and at each load it have different id. So I cannot disable those element in Jquery or javascript

Some people are very rude to down vote despite giving proper explanation.

I am using SharePoint 2013, it have its own master page where it add lot of elements which I don't have control, so I cannot individually disable them.

I will thank you to Krishna for pointing to correct direction.

His answer for ajax POST cannot help as I got error No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' . I don't have control over other domain, So I cannot use his answer.

So what I did is I created dynamically form add required parameter and then submit it.

Hope it can help somebody

$("#frm").remove(); /** remove extra frm before creating it **/
var frm = $('<form id="frm" action="' + _url + '" method="POST"></form>');
var params = a.split('?')[1].split('&'); /*custom string with key/value and sperated with & */
$.each(params, function (index) {
    var paramsV = params[index].split('=');
    frm.append('<input type="hidden" name="' + paramsV[0] + '" value="' + paramsV[1] + '" /> ');
});
frm.appendTo(document.body).submit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM