简体   繁体   English

如何在页面上运行GET / POST和JSON RESULT?

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

I have a page that does a JSON result, get and post method in the controller with two submit buttons. 我有一个页面执行JSON结果,并在控制器中使用两个提交按钮实现get和post方法。 One button goes to the Post method only for a redirect, and the other button goes to the JsonResult method(named AddTableData). 一个按钮转到Post方法仅用于重定向,另一个按钮转到JsonResult方法(名为AddTableData)。 How do I set this up in my JQuery code? 如何在我的JQuery代码中进行设置?

$('#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? 如何使用firstSubmit仅点击帖子,而第二个Submit仅使用JsonResult?

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. 我通常在get / post函数中获取的变量不见了。 How do I post them to my JSon function? 如何将它们发布到我的JSon函数中?

Assuming I am understanding you correctly I have 2 answers for you... 假设我正确地理解了您,我会为您提供2个答案...

  1. Why use JavaScript just to post a form without Ajax? 为什么只使用JavaScript发布没有Ajax的表单?

  2. If you must post the form using JavaScript, submit the form, not the document, so something like: 如果必须使用JavaScript发布表单,请提交表单,而不是文档,如下所示:

    $('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");
    });
});    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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