jquery - How to submit a same form with different url? -


in click of button form submit , success data contains next_url. goal submit same form next_url. possible.? sorry if question asked give me url.

my script code:

$(document).ready(function(){   $('form').submit(function(event){     event.preventdefault();     $.ajax({       type: 'post',       url: '/',       async: false,       data: $("#form_to_submit").serialize(),       success: function(data){         console.log(data);         $('form').attr('action', data.next_url);         $('form').unbind('submit').submit();       }     })     return false;   }) }) 

and sample django view here.

def home(request):     data_dict = {}     if request.method == 'post':         form_data = request.post['data']         data_dict['form_data'] = form_data         data_dict['next_url'] = reverse('frontend:first_term')         return jsonresponse(data_dict)     return render(request, 'index.html', {'data_list': data_list})   def first_term(request):     data_dict = {}     data_dict['next_url'] = reverse('frontend:second_term')     return jsonresponse(data_dict) 

update 1 :

above code redirect localhost.com/first_term , {"next_url": "/second_term"} context.

but expected first_term post value , next_url in success method [ third_term ]

you can try one:

$(document).ready(function() {   $('form').submit(function(event) {     event.preventdefault();     $.ajax({       type: 'post',       url: '/',       async: false,       data: $("#form_to_submit").serialize(),       datatype: 'json',        cache: false       success: function(data) {         console.log(data);         $('form').attr('action', data.next_url);         $('form').unbind('submit').submit();       }     })    }) }) 

or

see demo


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -