ajax post 在本地工作但在部署时不工作

标签 ajax asp.net-mvc

谁能看出我做错了什么,因为它让我有点生气(不确定是星期五的大脑还是婴儿的大脑)...

我有一个 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/

相关文章:

c# - ASP .NET C# MVC 5 代码优先外键属性/列名称

php - 在命令运行时从 shell_exec 命令获取输出

javascript - 从 ajaxResponse 下载 docx 文件

c# - 为带有子文件夹的 ASP.NET MVC 区域路由生成 URL

asp.net-mvc - Windows Azure 生产中的异常详细信息屏幕

c# - Asp.Net SignalR DependencyResolver 不解析集线器

c# - ASP.NET MVC3 : Set custom IServiceProvider in ValidationContext so validators can resolve services

php - 使用索引创建 PHP PDO 查询?

javascript - 使用 JQ 的 Ajax 请求在 Chrome 中出现错误 : "Failed to load resource"

javascript - 为什么当我将变量记录到控制台时我会得到输出,但是当我返回它时,我会收到 "Undefined"