this is an MVC application that I am currently working on. So there is a page in which we display a product details. On this page is a link clicking on which loads another page which shows a set of rules to process this product.
这是我目前正在研究的MVC应用程序。因此,我们会在其中显示产品详细信息的页面。在此页面上有一个链接点击,该链接会加载另一个页面,其中显示了一组处理此产品的规则。
Here's what I have tried now:
这是我现在尝试过的:
The controller, doesn't do much for now:
控制器,现在做的不多:
public class ProductRuleController : Controller
{
[HttpGet]
public ActionResult GetAllRulesForProduct(string productId)
{
return View("AddEditProductRule");
}
}
The view:
@{
ViewBag.Title = "Add / Edit Product Rule";
}
<h2>Add / Edit Rule</h2>
The JS
function ShowProductRules() {
var urlToGetProductRules = "/ProductRule/GetAllRulesForProduct/";
var productId = $('#ProductId').val();
if (productId == null) {
alert('Please Check the ProductId');
}
else {
// Make an ajax call passing in the ProductId
alert('ProductId: ' + productId);
$.ajax({
type: "Get",
url: urlToGetProductRules,
data: { productId: productId },
dataType: "json"
})
.done(function (data) {
})
.fail(function (xhr) {
alert("Something went wrong!!!")
console.log(xhr.responseText);
});
}
The ajax call goes into the .fail part. I inspected the responseText and that has the rquired HTML in it without any errors.
ajax调用进入.fail部分。我检查了responseText,其中包含了所需的HTML而没有任何错误。
So my question is when the response has the desired HTML in it why is it going to the Fail part? What am I doing wrong?
所以我的问题是,当响应中包含所需的HTML时,为什么它会转到Fail部分?我究竟做错了什么?
Thanks in advance.
提前致谢。
1 个解决方案
#1
remove the dataType: "json"
part from the ajax call
从ajax调用中删除dataType:“json”部分
that is
$.ajax({
type: "Get",
url: urlToGetProductRules,
data: { productId: productId },
})
.done(function (data) {
})
.fail(function (xhr) {
alert("Something went wrong!!!")
console.log(xhr.responseText);
});
When you specifies dataType : "json"
, the ajax call will expect a json response.If its html, it will count it as an error so the fail part will execute.If you still want to use dataType : "json"
, then convert the response to json format.
当你指定dataType:“json”时,ajax调用将期望一个json响应。如果是html,它会将其计为错误,因此失败部分将执行。如果你仍然想使用dataType:“json”,那么转换对json格式的响应。
#1
remove the dataType: "json"
part from the ajax call
从ajax调用中删除dataType:“json”部分
that is
$.ajax({
type: "Get",
url: urlToGetProductRules,
data: { productId: productId },
})
.done(function (data) {
})
.fail(function (xhr) {
alert("Something went wrong!!!")
console.log(xhr.responseText);
});
When you specifies dataType : "json"
, the ajax call will expect a json response.If its html, it will count it as an error so the fail part will execute.If you still want to use dataType : "json"
, then convert the response to json format.
当你指定dataType:“json”时,ajax调用将期望一个json响应。如果是html,它会将其计为错误,因此失败部分将执行。如果你仍然想使用dataType:“json”,那么转换对json格式的响应。