我在mvc 3中的帖子期间无法获得收集值.它返回null.
$.post("/Work/Post", { vm: $('#myForm').serializeArray(), 'collection': ['a', 'b', 'c'] });
//Or
var data = $('#myForm').serializeArray();
data.push({ name: 'collection', value: ['a', 'b', 'c'] });
$.post("/Work/Post", data);
//Or
var data = $('#myForm').serializeArray();
data.push({ name: 'collection[]', value: ['a', 'b', 'c'] });
$.post("/Work/Post", data);
Run Code Online (Sandbox Code Playgroud) 我想在点击提交按钮时验证WordPress帖子上的用户条目,显示有问题的错误消息,如果一切正常,则提交表单.我有一个PHP函数来执行检查,true如果数据输入form_data正常则返回,否则返回一些错误代码.以下JavaScript发布了AJAX请求,并且应该在成功检查后继续提交表单,但它不会:
jQuery(document).ready(function() {
jQuery('#post').submit(function() {
var form_data = jQuery('#post').serializeArray();
var data = {
action: 'ep_pre_submit_validation',
security: '<?php echo wp_create_nonce( 'pre_publish_validation' ); ?>',
form_data: jQuery.param(form_data),
};
var proceed = false;
jQuery.post(ajaxurl, data, function(response) {
if (response.indexOf('true') > -1 || response == true) {
proceed = true;
} else {
alert("Error: " + response);
proceed = false;
}
});
jQuery('#ajax-loading').hide();
jQuery('#publish').removeClass('button-primary-disabled');
return proceed; //breakpoint here makes the code run
});
});
Run Code Online (Sandbox Code Playgroud)
该代码改编自WPSE问题,该问题最初对我不起作用,因为表单未提交.我发现如果绑定的jQuery函数.submit()返回true,那么应该提交表单,这就是我试图实现的.使用上面的代码,它似乎一开始就不起作用(当没有错误时表单不会被提交),但是 …
我将jQuery.post的简单示例修改为
$("#searchForm").submit(function(event) {
event.preventDefault();
var $form = $( this ),
term = $( "input[name^=tick]:checked" ).serialize(),
url = $form.attr( 'action' );
$.post( url, { ticks: term, id: '55' },
function( data ) {
$( "#result" ).empty().append( data );
}
);
});
Run Code Online (Sandbox Code Playgroud)
这适用于单个复选框,val()但不适用于多个复选框
<input type="checkbox" name="tick" value="'.$value.'" />
Run Code Online (Sandbox Code Playgroud)
由于serialize() should generate刻度:术语to be used as术语in$.post`。
我怎样才能serialize()生成适当的数据$.post
注意:我不想序列化整个表单,而只想序列化复选框 INPUT 的值。
有没有办法将jquery post方法发送的数组反序列化为直接c#string数组(string [])?
我试着发布这样的数据
$.post(url,
{
'selectedTeams[]'=['Team1','Team2']
},
function(response) {}, 'json');
Run Code Online (Sandbox Code Playgroud)
并尝试在C#类中使用它
string jsonData = new StreamReader(context.Request.InputStream).ReadToEnd();
var selectedTeams = new JavaScriptSerializer().Deserialize<string[]>(jsonData);
Run Code Online (Sandbox Code Playgroud)
由于字符串[]中没有属性selectedTeams [],因此它不起作用而且不应该来源.
我知道定义类这样的类的方法
class Teams
{
public string[] SelectedTeams{get;set;}
}
Run Code Online (Sandbox Code Playgroud)
然后进行反序列化.
但我认为这是一个不必要的定义类,所以没有办法直接将json数组转换为c#字符串数组
提前致谢.
我知道这个问题被问了数百次:(但我只是想了解更多:)。
我的问题很简单,我可以将一个值传递给这样的 js 文件,如果没有,如何传递?
<script type="text/javascript" src="./js/create.js?method=create"></script>
Run Code Online (Sandbox Code Playgroud)
是的,您注意到我有一个method=create要在我的create.js.
我知道在 jquery ajax 中,我们有一个简单的方法,但是您必须注意到 ajax 方法包含在 js 文件中,我如何将参数传递给 js 文件本身?
欢迎任何答案:) 谢谢。
一个jQuery AJAX请求.post()的数据到page.php,它创建了$ res和var_dump().
$ RES:
$res = array();
foreach ($_REQUEST as $key => $value) {
if($key){
$res[$key] = $value;
}
}
Run Code Online (Sandbox Code Playgroud)
后续代码var_dump($ RES):
array(4) {
["text1"]=> string(6) "mattis"
["text2"]=> string(4) "test"
["tu"]=> string(32) "deb6adbbff4234b5711cc4368c153bc4"
["PHPSESSID"]=> string(32) "cda24363cb9d3226bd37b2577ed0bc0b"
}
Run Code Online (Sandbox Code Playgroud)
我的javascript只发送text1和text2:
$.post("page.php",{
text1:"mattis",
text2:"test"
}
Run Code Online (Sandbox Code Playgroud)
什么是"tu"变量发送?显然它与会话ID非常相似,但我以前从未见过它.
编辑:它是在IE中发送但不在FF中发送.
我有一个ASP.Net MVC应用程序.我试图将数据发布到一个动作,然后只是渲染输出.我有一个页面,您可以在其中查看产品,因此www.mysite.com/product/details/1显示了该产品.在这个页面上,我通过RenderAction()渲染一个包含一些定价逻辑的局部视图.我希望用户选择一些选项,然后让jquery发布选项以获得新价格.
但是,当我打电话的时候
$.post({
url : "price/getprice",
data : {"options" : chosenOptions},
success : function(data) {
$("#price").text(data);
}
});
Run Code Online (Sandbox Code Playgroud)
该网址发布到该网址www.mysite.com/product/details/price/getprice不存在.我已经尝试发布到"〜/ price/getprice /",但它做了同样的事情.为什么不去我的PriceController的GetPrice(FormCollection表单)动作?
这应该发布到www.mysite.com/price/getprice.当我查看firebug中的Net选项卡时,它说它发布了这个:
http://localhost:42427/Product/Details/%5Bobject%20Object%5D
Run Code Online (Sandbox Code Playgroud)
如果我查看Firebug中的响应,应用程序将抛出异常:
参数字典包含非可空类型'System.Int32'的参数'id'的空条目,用于'PrintPlaceWebsite.Controllers.ProductController'中方法'System.Web.Mvc.ActionResult Details(Int32)'.
但是我不是要试着去那个地方所以......呃.
我已经在应用引擎上工作了一段时间,从那以后这个问题一直困扰着我.无法找到解决方案,所以我想问过.
我在app引擎服务器的python中有一个简单的post处理程序.我正在做一个jQuery帖子.两者的代码都是这样的
main.py
import json
...
class SomeHandler(webapp2.RequestHandler):
def post(self):
data = json.loads(self.request.body)
return self.response.out.write(json.dumps(data))
Run Code Online (Sandbox Code Playgroud)
和jQuery帖子
jQuery.post('/quiz',
{name:'some problem 2',desc:'some submitted 2',questions:[{question:'question1'}]},
function(data,textStatus, jqXHR){console.log('POST response: ');console.log(data);});
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我得到以下错误
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File …Run Code Online (Sandbox Code Playgroud) 当我们使用$ .post时,我使用断点并发现我的Page_Load事件再次触发.那么$ .post的用途是什么,这是部分回发吗?
我很困惑,如果Page_Load事件在两种情况下都会触发(服务器端调用,$ .post)那么$ .post的优点是什么?
jquery-post ×9
jquery ×8
javascript ×3
json ×2
post ×2
ajax ×1
asp.net ×1
asp.net-mvc ×1
php ×1
python-2.7 ×1
var-dump ×1