jQuery将数据发布到错误的相对URL

sco*_*ttm 1 asp.net-mvc jquery jquery-post

我有一个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)'.

但是我不是要试着去那个地方所以......呃.

Fel*_*ing 5

那么你指定了一个相对的URL,所以如果这个代码在现场www.mysite.com/a/b/c,URL将指向/a/b/price/getprice.

您可能想要指定绝对URL:

$.post({
  url : "/price/getprice",
  //     ^-- leading slash
  data : {"options" : chosenOptions},
  success : function(data) {
              $("#price").text(data);
            }
});
Run Code Online (Sandbox Code Playgroud)