阿贾克斯邮政参数始终是在MVC应用程序空 [英] Ajax Post Parameter is Always Null in MVC app
问题描述
我有一个简单的职位,在一个测试应用程序的工作原理
I have a simple post that works in a test app
控制器code
public ActionResult Delete(string Id)
{
... delete record ...
}
JavaScript的
JavaScript
function delAnnouncement(id) {
$.ajax({
type: "POST",
url: "@Url.Action("Delete", "Announcements")",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ "Id": "03d23684-098a-4ae8-8fa2-7d9ce70d63ef" }), // Hard coded for testing
success: function (data) {
$("#AnnouncementsPH").replaceWith(data);
}
});
}
我创建了一个简单的测试应用程序,它在那里工作。
I have created a simple test application and it works there.
我要寻找任何想法,以什么可以在现有的应用程序,总是会使传递给为空的ID存在。
I am looking for any ideas as to what may exist in the existing application that would always cause the id passed to be null.
东西我曾尝试:
路由信息是在两个应用程序之间的相同
他们都是安全的HTTPS网站。
预期对现有的网站工作的其他形式。这是只使用一个javascript后带参数的地方。这仅仅是列出的任何系统通知新的视图。
至于我可以告诉HTML是有效的
提琴手显示正在发送的数据。
我试图根本就很难code在ID一通
Things I have tried: Route Info is the same between the two applications They are both secure https websites. Other forms on the existing website work as expected. This is the only place a javascript post with a parameter is used. It is just the new view that lists any system announcements. As far as I can tell the HTML is valid Fiddler show the data is being sent. I have tried to simply hardcode in the Id to pass
我不知道这可能是导致后不工作。
I have no idea what could be causing the Post not to work.
感谢您的任何建议。
编辑1:这里是小提琴手原岗位
Edit 1: here is the fiddler Raw post
POST https://localhost:44300/Announcements/Delete HTTP/1.1
Host: localhost:44300
Connection: keep-alive
Content-Length: 39
Accept: */*
Origin: https://localhost:44300
X-Requested-With: XMLHttpRequest
Content-TypeOfNotification: application/json; charset=utf-8
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
Content-Type: text/plain;charset=UTF-8
Referer: https://localhost:44300/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: jetbrains.charisma.main.security.PRINCIPAL=NmI4YmFhZTExMThjZDZkZjNmZTBhMDNiZWM4NmY3MDYzZWNiMWE3M2ZmZDM5ODMwYjViMjczN2ZkZmU3YmZlZTpyb290; TCSESSIONID=233EBE63123BA35468235F441F54D7E4; ASP.NET_SessionId=ycnx1ejeyahzipwduux2quqz; __RequestVerificationToken=FnOKGFfBZKEBX4E0KBoV9133e5KK3h9Pd_OjDzNPjv7ifMTMk5uHUPmT621hOQFyOCwW5IhdewKLPDMs2_9jY2vVwrHLYOU9f0F86mN-NoQ1; .AspNet.ApplicationCookie=V8ZnbAx_2_H1Wx946VLcQ235XplzC-uvKdw4CP7Sm8ZVnJu9pG63EuzC0ptOZZNBvzZkRjB0RJS25Pn1WHOzeQSrqoWl87keqRDS6_vMwZ9L6PgKU0rJz7OhD7eKps8l3tzR097zI5WbU_chUZFKLLw1c__rfN3Fy6BbHC1qNtgx0C86AShhG5EsNiruYqJZn-Uj7Z2h75YcZctCFniMyuzD-9RetcMjkN3_PbAJg-_urfntG9NwsMEQdBf1b3K9H3GP_wUYRhnbQdNZpuAkAEa6bVfJiHrnKFhnhLkP8BAfocbMKES1wQKbXBfcNn62cEKUu3On3lHNCNN4zEvOhxF1aDaBk-yghOtvtNkROeFTKMQFD0U-XXAf-RKm0Nwgp1Tb2Ip2U42vshDRNGXQSkOojisVUxiPmkrxDtckNycQd0br1cFSqxfeXyg3cg_vKUP1VMBQcBQxZO6MVLSCDdcpANyoE43IoLp3BHgURJZP88vF18JfVV646XGOZ3QU
Id=03d23684-098a-4ae8-8fa2-7d9ce70d63ef
正如你所看到的标识被张贴到控制器,只是没有解析并提供给该方法的参数。
As you can see the Id is posted to the controller, just not parsed and supplied to the parameter in the method.
推荐答案
您可以添加喜欢的参数:
You can append the parameter like:
var targeturl = '@Url.Action("Test", "Controller")?id=' + ID;
$.ajax({
url: targeturl,
type: "GET",
success: function(data) { },
error: function (data) { }
});
或者你可以直接使用jQuery的ajax的数据参数:
Or you may use the jQuery ajax data parameter directly:
$.ajax({
type: "POST",
url: '@Url.Action("Test", "Controller")',
data: { id: "your-id" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
参考: http://api.jquery.com/jquery.ajax/
这篇关于阿贾克斯邮政参数始终是在MVC应用程序空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!