谁能看出我做错了什么,因为它让我有点生气(不确定是星期五的大脑还是婴儿的大脑)...
我有一个 Ajax 帖子,它在本地运行良好,但当我部署到另一台服务器时却无法运行。
我已经放入了很多警报以确保我得到了我期望的参数,所以不太确定缺少什么。
这是 ajax 帖子:
查看:
var theUrl = "/Widgets/TestBin2AutomationResults/" + widgetImpressionId + "/" + retailerProductId + "/" + quantity;
alert("widgetImpressionId" + widgetImpressionId);
alert("retailerProductId" + retailerProductId);
alert("quantity" + quantity);
alert(theUrl);
$("#imgaddtocart").hide();
$("#addMe").unbind('click');
$("#delete").unbind('click');
$.ajax({
type: "POST",
url: theUrl,
data: { 'username': username, 'password': password },
dataType: "json",
success: function (data) {
if (data != null) {
alert("we are inside data");
Controller :
[JsonpFilter]
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult TestBin2AutomationResults(int widgetImpressionId, int retailerProductId, int quantity, string username, string password)
{
MessageBox.Show("We are inside TestBin2Automation controller " + widgetImpressionId + "/" + retailerProductId + "/" + quantity + "/" + username + "/" + password);
全局.asax
routes.MapRoute("Bin2SubmitTestBin2Automation", "Widgets/TestBin2AutomationResults/{widgetImpressionId}/{retailerProductId}/{quantity}", new { controller = "Widgets", action = "TestBin2AutomationResults", widgetImpressionId = 0, retailerProductId = 0, quantity = 0, username = "", password = "" });
我没有进入 Controller ,因为 MessageBox.Show 没有显示。
任何帮助都很感激,如果整个周末都没有这个悬在我头上就好了!
非常感谢
最佳答案
我在我的 mvc 项目中也遇到了这个问题....
我通过以下步骤解决了这个问题。
1) 把它放在脚本部分的布局页面中
<script type="text/javascript">
var RootUrl = '@Url.Content("~/")';
</script>
2) 在 ajax url 中添加“RootUrl”变量。 (它也在你的 Js 文件中工作,你在你的 ajax url 之前添加“RootUrl”)
var theUrl = RootUrl +"Widgets/TestBin2AutomationResults/" + widgetImpressionId + "/" + retailerProductId + "/" + quantity;
它对我来说很完美,但如果有人有其他解决方案,请发邮件给我
关于ajax post 在本地工作但在部署时不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19862499/