jQuery的POST请求(不AJAX)

编程入门 行业动态 更新时间:2024-10-27 08:36:26
本文介绍了jQuery的POST请求(不AJAX)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在一个ASP.NET MVC应用程序使用jQuery上发布的数据按钮,单击:

In an ASP.NET MVC app I use jQuery for posting data on button-click:

<button onclick="addProducts()">Add products</button> .... $.post('<%= Url.Action("AddToCart", "Cart") %>', { ... returnUrl: window.location.href });

在购物车控制器AddToCart行动我使用重定向到另一个View发布后:

In the "AddToCart" action of "Cart" controller I use redirection to another View after posting:

public RedirectToRouteResult AddToCart(..., string returnUrl) { ... return RedirectToAction("Index", new { returnUrl }); }

一切都还好,除了这种重定向。我留在同一页上张贴后。我怀疑这是由于AJAX类型POST请求。

All is okay, except this redirection. I stay on the same page after posting. I suspect it's due to AJAX type of "POST" request.

如何用jQuery POST请求拦截重定向解决这个问题?

How to solve the problem with jQuery POST request blocking the redirection?

推荐答案

我创建了一个 $。表(网址[,数据[,方法='POST'])函数创建一个隐藏的形式,用指定的数据填充它,并将其附加到&LT;身体GT; 。下面是一些例子:

I created a $.form(url[, data[, method = 'POST']]) function which creates a hidden form, populates it with the specified data and attaches it to the <body>. Here are some examples:

$.form('/index') <form action="/index" method="POST"></form>

$.form('/new', { title: 'Hello World', body: 'Foo Bar' }) <form action="/index" method="POST"> <input type="hidden" name="title" value="Hello World" /> <input type="hidden" name="body" value="Foo Bar" /> </form>

$.form('/info', { userIds: [1, 2, 3, 4] }, 'GET') <form action="/info" method="GET"> <input type="hidden" name="userIds[]" value="1" /> <input type="hidden" name="userIds[]" value="2" /> <input type="hidden" name="userIds[]" value="3" /> <input type="hidden" name="userIds[]" value="4" /> </form>

$.form('/profile', { sender: { first: 'John', last: 'Smith', postIds: null }, receiver: { first: 'Foo', last: 'Bar', postIds: [1, 2] } }) <form action="/profile" method="POST"> <input type="hidden" name="sender[first]" value="John"> <input type="hidden" name="sender[last]" value="Smith"> <input type="hidden" name="receiver[first]" value="John"> <input type="hidden" name="receiver[last]" value="Smith"> <input type="hidden" name="receiver[postIds][]" value="1"> <input type="hidden" name="receiver[postIds][]" value="2"> </form>

使用jQuery的 .submit()方法,你可以创建并提交表单用一个简单的前pression:

With jQuery's .submit() method you can create and submit a form with a simple expression:

$.form('stackoverflow/search', { q: '[ajax]' }, 'GET').submit();

下面的函数定义:

jQuery(function($) { $.extend({ form: function(url, data, method) { if (method == null) method = 'POST'; if (data == null) data = {}; var form = $('<form>').attr({ method: method, action: url }).css({ display: 'none' }); var addData = function(name, data) { if ($.isArray(data)) { for (var i = 0; i < data.length; i++) { var value = data[i]; addData(name + '[]', value); } } else if (typeof data === 'object') { for (var key in data) { if (data.hasOwnProperty(key)) { addData(name + '[' + key + ']', data[key]); } } } else if (data != null) { form.append($('<input>').attr({ type: 'hidden', name: String(name), value: String(data) })); } }; for (var key in data) { if (data.hasOwnProperty(key)) { addData(key, data[key]); } } return form.appendTo('body'); } }); });

更多推荐

jQuery的POST请求(不AJAX)

本文发布于:2023-10-12 01:09:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483334.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:jQuery   POST   AJAX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!