How to add form value in jQuery?

I work with jQuery form and ASP.NET MVC. Preview 5 ASP.NET MVC has an extension method for HttpRequest called IsAjaxMvcRequest that determines if POST has been requested by Ajax. This extension method basically “sniffs” a form value called __MVCASYNCPOST and (mostly) returns true if it sees this element.

What I want to do is paste this value using a script (I cannot use a hidden field because it defeats the target) in the form message - and I don't know how to do it with jQuery.

Here is my code:

 <script type="text/javascript"> $(document).ready(function() { $('#jform').submit(function() { //add a flag for asynch submit //" __MVCASYNCPOST $('#jform').ajaxSubmit({ target: '#results2' }); return false; }); }); </script> 

I really need to know how to do this :), but I do not! In addition, code savings are required here, so the LOC score is evaluated.

+4
source share
1 answer

ajaxSubmit() takes a data parameter with additional key-value pairs to send.


Also, there is a better way to check the server side if the request is an AJAX request. jQuery sets the X-Requested-With HTTP header to XMLHttpRequest . You can change your extension method to test this instead of a custom field.

+8
source

Source: https://habr.com/ru/post/1277253/


All Articles