【问题标题】:How can I use ajax jquery to redirect to another page controller action如何使用 ajax jquery 重定向到另一个页面控制器操作
【发布时间】:2015-03-04 12:46:11
【问题描述】:

如何使用 ajax 重定向到具有参数 id 和 name 的另一个控制器操作,但这些参数不显示在 url 中?

<form class="bisForm" id="bisForm" name="bisForm" method="post">
  <div class="contentOne">
    {% for index, b in bis|json_decode %}
      <input class="button-primary toBis" type="button" value={{ b }} id={{ "bis_"~index }}>
    {% endfor %}
  </div> </form>

  $(document).ready(function(){     
    $('.toBis').click(function(event){
      event.preventDefault();
      var bisId   = event.currentTarget.id;
      var bisName = event.currentTarget.value;

      //console.log(bisName.serialize());
      $.ajax({
        // contentType: 'application/json; charset=utf-8',
        type: "post",
        url: "/bis/content/",
        data: {name: bisName, id: bisId},
        //data: "name="+ bisName+"&id="+ bisId,
        contentType: 'application/json; charset=utf-8',
        //dataType: 'json',
        //data: "name="+bisName,
        cache: false,
        async: false,
        error: function(response){
          if(!response)
            alert("Server Error");
          },
        success: function(data){
          //console.log(data);
          //window.location.href = '/bis/content/';
        }
      });

      return false;
    });
  });

【问题讨论】:

  • 使用edit-按钮向您的问题添加内容
  • 是的,明白了。然后,您还需要一个 Ajax-post 来成功完成主要的 Ajax 调用。并将参数发布到'/bis/content/'。谢谢。
  • 你好,请问你有什么例子吗?

标签: jquery ajax json phalcon


【解决方案1】:

不要使用 AJAX 发布数据,只需提交表单即可。在提交表单之前有一个隐藏字段并为其分配点击按钮 ID 和 VALUE (也有表单提交数据的动作属性)。喜欢,

<form class="bisForm" id="bisForm" name="bisForm" method="post" action="/bis/content/">
  <div class="contentOne">
    {% for index, b in bis|json_decode %}
      <input class="button-primary toBis" type="button" value={{ b }} id={{ "bis_"~index }}>
     <input type="hidden" name="clickedItem" id="clickedItem" value="" />
    {% endfor %}
  </div> </form>

在 JavaScript 代码中,进行这些更改,

$(document).ready(function(){     
    $('.toBis').click(function(event){
      event.preventDefault();
      var bisId   = event.currentTarget.id;
      var bisName = event.currentTarget.value;      
      $('#clickedItem').attr('value', bisId +'$'+ bisName); // assign value here
      $('#bisForm').submit(); // submit the form
      return false;
    });
  });

在您的控制器操作中,获取已提交的隐藏值,将其除以 '$' 并根据需要使用它们。

[httppost]
public ActionResult content(string clickedItem)
{
  string[] clickedItems = clickedItem.split('$');
  string id = clickedItems[0].toString();
  string value = clickedItems[1].toString();
}

希望对你有帮助。

【讨论】:

    【解决方案2】:

    在你的 ajax 调用“成功”时使用它:

     success: function(data) {
        var strUri = window.location.href;
        window.location.href = strUri.replace(window.location.pathname, '/bis/content/');
    }
    

    希望它有效。谢谢。

    【讨论】:

    • Thx,但是当我转到 bis 页面时,我想获取所有参数,这是我的问题,所以我想在 url homePage/bis/content/?在 bis 的控制器操作中,我可以获取我的参数名称和 id
    • 也许我犯了一个错误,当我点击这个按钮时我想重定向到 url /bis/content/ 并发布我的参数而不在我的 url 中显示它们我怎么能用 Ajax 和json
    • @chall,那你为什么不直接从服务器端代码重定向呢?使用 RedirectToAction 并传递参数。
    • 因为我不知道如何使用它,只是我想重定向到这个 url /bis/content/ 并发布我的参数名称和 id 而不在 url 中显示它们以确保模式安全,所以,我需要一个解决方案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2012-10-10
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    相关资源
    最近更新 更多