Javascript jqueryajax调用不';t点击操作方法/返回整个HTML页面 我的问题的解决方法可以在下面找到。
我无法让我的代码工作。我需要向控制器中的操作方法发送一个AJAX调用,该方法必须返回一个字符串(目前)。但是当我激活AJAX调用时,它返回我当前所在页面的HTML源代码,甚至没有执行所需的控制器操作Javascript jqueryajax调用不';t点击操作方法/返回整个HTML页面 我的问题的解决方法可以在下面找到。,javascript,jquery,ajax,asp.net-mvc,Javascript,Jquery,Ajax,Asp.net Mvc,我无法让我的代码工作。我需要向控制器中的操作方法发送一个AJAX调用,该方法必须返回一个字符串(目前)。但是当我激活AJAX调用时,它返回我当前所在页面的HTML源代码,甚至没有执行所需的控制器操作 这是我的代码: Ajax调用 如果我甚至能够简单地为StoreProductInSession操作提供ID,我就可以从那里开始工作。我将使用用户会话,因此我认为GET不够安全 解决方案 非常感谢@Dolan的帮助!问题是我的路线 Ajax调用 路由 尝试此操作,删除“dataType:html”并更
这是我的代码: Ajax调用 如果我甚至能够简单地为StoreProductInSession操作提供ID,我就可以从那里开始工作。我将使用用户会话,因此我认为GET不够安全 解决方案 非常感谢@Dolan的帮助!问题是我的路线 Ajax调用 路由 尝试此操作,删除“dataType:html”并更改:(echo改为return):
您应该使用URL帮助器:
url: '@Url.Action("storeProductInSession", "Product")',
有关详细信息,请参阅
好的,我认为你的产品对象有问题。它不是一个对象,而是一个字符串。问题在于产品对象周围的“字符”——JavaScript认为它是字符串——而不是对象。应该是这样的:
var product = {
"id": "@Model.ID",
"naam": "@Model.Naam",
"afbeelding": "@Model.Afbeelding",
"beschrijving": "@Model.Beschrijving",
"prijs": "@Model.Prijs",
"aantal": " + amount + "
};
请看这里:
此外,如果您知道id将是一个整数,则可以执行以下操作:
var product = {
"id": @Model.ID,
"naam": "@Model.Naam",
"afbeelding": "@Model.Afbeelding",
"beschrijving": "@Model.Beschrijving",
"prijs": "@Model.Prijs",
"aantal": " + amount + "
};
你能试试这个吗
[HttpPost]
public string storeProductInSession(object product)
{
return "This method is going to do session stuff, but for now it just has to return this string";
}
看看你们的路线,我能看出问题所在
假设storeProductInSession是您要查找的产品的id,因此转到产品控制器的索引操作
我们需要的是约束。约束将告诉路由仅在id为数字(数字)时使用它。例如:约束:新的{id=@“\d+”}
因此,您需要将路由更改为以下内容(您还需要Product/storeProductInSession操作的路由):
因此,当您请求Product/storeProductInSession时。它将看到storeProductInSession不是一个数字,然后转到下一个路径
下一个路径将指向产品控制器和storeProductInSession操作。是否存在用于测试的任何位置?你能直接在浏览器中访问/Product/storeProductInSession/并得到结果吗?@DonBoots这是一篇文章,所以我必须做一些修改来测试它。不过,请注意:我尝试使用我的“test”操作,它也只返回一个“it works”字符串(我在开发的早期使用过这个字符串),ajax调用也没有达到这个操作,它仍然为此打印整个HTML页面+1!这将更好地发挥您的路由设置。你需要改变路线的地方越少越好@Donal,它仍然返回HTML页面,你还有其他好的技巧:)?@S.tenBrinke好的,我认为问题在于你的产品对象定义。JavaScript将其视为字符串,而不是对象。我已经用更多信息更新了答案。@Donal不!仍然是整个页面。我尝试将数据类型更改为json,但它仍然返回“意外
[HttpPost]
public string storeProductInSession(int id, string naam, string afbeelding, string beschrijving, decimal prijs, int aantal)
{
return String.Format("Product given to the ajax call: {0}, {1}, {2}, {3}, {4}, {5}", id, naam, afbeelding, beschrijving, prijs, aantal);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Product",
url: "Product/{id}",
defaults: new { controller = "Product", action = "Index" },
constraints: new { id = "\\d+" }
);
routes.MapRoute(
name: "ProductAction",
url: "Product/storeProductInSession",
defaults: new { controller = "Product", action = "storeProductInSession" }
);
routes.MapRoute(
name: "Categorie",
url: "Categorie/{id}",
defaults: new { controller = "Categorie", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Welkom", action = "Index", id = UrlParameter.Optional }
);
}
[HttpPost]
public string storeProductInSession(int id, string naam, string afbeelding, string beschrijving, decimal prijs, int aantal)
{
echo "This method is going to do session stuff, but for now it just has to return this string";
}
url: '@Url.Action("storeProductInSession", "Product")',
var product = {
"id": "@Model.ID",
"naam": "@Model.Naam",
"afbeelding": "@Model.Afbeelding",
"beschrijving": "@Model.Beschrijving",
"prijs": "@Model.Prijs",
"aantal": " + amount + "
};
var product = {
"id": @Model.ID,
"naam": "@Model.Naam",
"afbeelding": "@Model.Afbeelding",
"beschrijving": "@Model.Beschrijving",
"prijs": "@Model.Prijs",
"aantal": " + amount + "
};
[HttpPost]
public string storeProductInSession(object product)
{
return "This method is going to do session stuff, but for now it just has to return this string";
}
routes.MapRoute(
name: "Product",
url: "Product/{id}",
defaults: new { controller = "Product", action = "Index" },
constraints: new { id = "\\d+" }
);
routes.MapRoute(
name: "ProductAction",
url: "Product/storeProductInSession",
defaults: new {controller = "Product", action = "storeProductInSession"}
);
routes.MapRoute(
name: "Categorie",
url: "Categorie/{id}",
defaults: new { controller = "Categorie", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Welkom", action = "Index", id = UrlParameter.Optional }
);