I have an Ajax call which returns an JSON object, within the object is a property that states if the call was a success or not. For some reason, even if the call comes back as a success, the AJAX never hits it, in-fact it always returns as a failure. Not sure why.
我有一个返回JSON对象的Ajax调用,在该对象中是一个属性,表明调用是否成功。出于某种原因,即使呼叫恢复成功,AJAX也从未触及它,事实上它总是以失败告终。不知道为什么。
AJAX:
function GetSubTaskStories() {
$.ajax({
type: "POST",
url: "Story.aspx/GetSubTaskStories",
data: JSON.stringify({ id: GetSubTaskID() }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if (data.success) {
alert("Success: True");
} else {
alert("Success: False");
}
},
error: function (data) {
}
});
}
SERVER:
[WebMethod]
public static string GetSubTaskStories(string id)
{
// Do some stuff.
JavaScriptSerializer jss = new JavaScriptSerializer();
try
{
var storiesObj = new { success = true, stories = stories };
return jss.Serialize(storiesObj);
}
catch (Exception ex)
{
var error = new { success = false };
return jss.Serialize(error);
}
}
3 个解决方案
#1
1
You're returning an object with a property d
that is a string that contains the serialized version of your result. It's not easy to see from your example with all the quotes and escaped quotes.
您将返回一个对象,该对象具有属性d,该属性是包含结果的序列化版本的字符串。从您的示例中看到所有引号和转义引号并不容易。
Remove the jss.serialize
and return the anonymous object as your result.
删除jss.serialize并返回匿名对象作为结果。
[WebMethod]
public static object GetSubTaskStories(string id)
{
// Do some stuff.
try
{
var storiesObj = new { success = true, stories = stories };
return storiesObj;
}
catch (Exception ex)
{
var error = new { success = false };
return error;
}
}
#2
0
Based on the comments, I think you need to change your success function to:
根据评论,我认为您需要将您的成功功能更改为:
success: function (data) {
if (data.d.success == true) {
alert("Success: True");
} else {
alert("Success: False");
}
}
in order to reflect the structure of the JSON being returned from the server
以反映从服务器返回的JSON的结构
#3
0
<script>
function GetSubTaskStories() {
$.ajax({
type: "POST",
url: "GetSubTaskStories",
data: JSON.stringify({ id: 5 }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(Boolean(data.success));
if (Boolean(data.success)) {
alert("Success: True");
} else {
alert("Success: False");
}
},
error: function (data) {
}
});
}
尝试使用布尔函数
#1
1
You're returning an object with a property d
that is a string that contains the serialized version of your result. It's not easy to see from your example with all the quotes and escaped quotes.
您将返回一个对象,该对象具有属性d,该属性是包含结果的序列化版本的字符串。从您的示例中看到所有引号和转义引号并不容易。
Remove the jss.serialize
and return the anonymous object as your result.
删除jss.serialize并返回匿名对象作为结果。
[WebMethod]
public static object GetSubTaskStories(string id)
{
// Do some stuff.
try
{
var storiesObj = new { success = true, stories = stories };
return storiesObj;
}
catch (Exception ex)
{
var error = new { success = false };
return error;
}
}
#2
0
Based on the comments, I think you need to change your success function to:
根据评论,我认为您需要将您的成功功能更改为:
success: function (data) {
if (data.d.success == true) {
alert("Success: True");
} else {
alert("Success: False");
}
}
in order to reflect the structure of the JSON being returned from the server
以反映从服务器返回的JSON的结构
#3
0
<script>
function GetSubTaskStories() {
$.ajax({
type: "POST",
url: "GetSubTaskStories",
data: JSON.stringify({ id: 5 }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(Boolean(data.success));
if (Boolean(data.success)) {
alert("Success: True");
} else {
alert("Success: False");
}
},
error: function (data) {
}
});
}
尝试使用布尔函数