如何对不同的MVC控制器进行Ajax调用 [英] How to make ajax calls to different MVC controllers
问题描述
在对我的MVC控制器进行Ajax调用时出现错误404
I'm getting error 404 while making ajax calls to my MVC controller
这是场景:
我有2个控制器:(PartnerControler,GettingSartedController)
I have 2 controllers:(PartnerControler,GettingSartedController)
在入门页面上:http://mysite/GettingStarted/
我进行了以下ajax调用:
While on the getting started page:http://mysite/GettingStarted/
I make the following ajax call:
$scope.SubmitRegistration = function() {
return $http({
url: '/partner/register',
method: 'POST',
data: $scope.UserAccount,
headers: {
'Content-Type': 'application/json'
在调用控制器时,我得到了404.
I get a 404 on the call to the controller.
当我将确切的注册方法添加到Getting Started控制器中并将ajax URL更改为以下代码时,它会起作用:
when I add the exact register method to the gettingstarted controller and change the ajax URL to the following it works:
url: '/gettingstarted/register',
在网络上使用特定控制器的页面时,您可以呼叫另一个控制器吗?
When on web a page that uses a specific controller can you make calls to another controller?
如何通过修改上面的脚本来实现?
How can this be achieved modifying the above script?
推荐答案
如果您有单独的js文件,则可以使用以下解决方法
If you have separate js files, you can use workaround like below
<input type="hidden" id="registerurl" data-register-url="@Url.Action("register", "partner")"/>
并将此element's attribute
用作url
以便在http
通话中使用
and use this element's attribute
as url
to be used in your http
call
var urlRegisterPartner = $('#registerurl').data('register-url');
这篇关于如何对不同的MVC控制器进行Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!