开发者

How do I run a GET/POST and JSON RESULT on a page?

开发者 https://www.devze.com 2023-01-12 02:21 出处:网络
I have a page that does a JSON result, get and post method in the controller with two submit buttons.One button goes to the Post method only for a redirect, and the other button goes to the JsonResult

I have a page that does a JSON result, get and post method in the controller with two submit buttons. One button goes to the Post method only for a redirect, and the other button goes to the JsonResult method(named AddTableData). How do I set this up in my JQuery code?

$('#firstSubmit').click(function() {
    $(document).submit(function() {
    });
});

$('#secondSubmit').click(function() {
    $('#addTable').submit(function() {
  开发者_如何转开发      $.post($(this).attr("action"), $(this).serialize(), function(response) {
            ...load Table
        });
        return false;
    });
});

<%using (Html.BeginForm("LoadTable", "Home", FormMethod.Post, new { id = "addTable" })) %>
    <td><input id="Submit1" type="submit" name="firstSearch" value="Search" /></td>
    <td><input id="Submit2" type="submit" name="secondSearch" value="Search" /></td>

How do I use the firstSubmit to hit the post only, and the second submit to work the JsonResult only?

EDIT

$('#secondSubmit').click(function() {
    $.getJSON("Home/AddTableData", {Name:"Name on Document"}, function(json) {
        alert("good");
    });
});    

My variables that usually get picked up on a get/post function is gone. How do I post them to my JSon function?


Assuming I am understanding you correctly I have 2 answers for you...

  1. Why use JavaScript just to post a form without Ajax?

  2. If you must post the form using JavaScript, submit the form, not the document, so something like:

    $('form').submit();


$('#firstSubmit').click(function() {
  $('#addTable').submit();
});

$('#secondSubmit').click(function() {
  $.getJSON("Home/AddTableData", $('#addTable').serialize(), function(json) {
    alert("good");
  });
});


<%using (Html.BeginForm("LoadTable", "Home", FormMethod.Post, new { id = "addTable" })) %>
<td><input id="Submit1" type="button" name="firstSearch" value="Search" /></td>
<td><input id="Submit2" type="button" name="secondSearch" value="Search" /></td>


I have not tested this:

$('#secondSubmit').click(function() {
    $url = 'Home/AddTableData/?'+$('#add_table').children('input').serialize()
    $.getJSON($url, {Name:"Name on Document"}, function(json) {
        alert("good");
    });
});    
0

精彩评论

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

关注公众号