MVC 5 + $ Post()函数在托管服务器后无法正常工作

我在MVC5中开发了一个应用程序。 在下面的代码中点击链接的onclick被调用 –

// Code in View File $.post('../ControllerName/FunctionName', //this is your url { id: image, }, function (data) { alert("Successfully published"); } ).error(function () { alert("Failed to publish"); }); //Code in Controller [HttpPost] public void ISPPDF(string id) {} 

我面临的问题是ISPPDF()函数在我通过visual studio运行时被调用。但是,在我在服务器上托管我的应用程序后,它似乎没有调用该函数..

我觉得我指定的路径存在一些问题 –

我也尝试过以下方式指定路径,但没有运气!

  /ControllerName/FunctionName ControllerName/FunctionName 

任何帮助,将不胜感激。

谢谢,

试试这个,因为你的post方法在视图文件中

$ .post(’../ FunctionName’,{id:image,},function(data){alert(“Successfully published”);})。error(function(){alert(“Failed to publish”);} );

你永远不应该在MVC中硬编码URL。

而是使用@Url.Action

 $.post('@Url.Action("FunctionName", "ControllerName")', //this is your url 

如果你需要发送参数,你可以这样做:

 $.post('@Url.Action("FunctionName", "ControllerName", new { id = Model.ID })', //this is your url 

我推荐这个有两个重要原因:

1.错误输入URL的可能性很大。 这个问题certificate了这一点,OP错误地输入了URL。
2. Url.Action会考虑您的路线。 如果您的路线发生变化, Url.Action将知道如何构建正确的url。 这样,您就不必通过多个视图来更改所有硬编码值。