我希望返回文件内容,并且我正在使用 Web 服务和 JSON 来执行此操作。
我已成功返回内容,但它的格式不是我想要的格式。
我需要顶部文本区域的格式与底部文本区域的格式相同:
我需要将 \n
显示为新行,而不是仅仅将其从字符串中删除。
当前代码(忽略警报,仅用于我自己的测试目的):
function CallWebService(CSSFile) {
var stringToDisplay = "";
var webMethod = "../services/BrokerAdmin/CSSComparison.asmx/GetFileContents";
$.ajax({
type: "POST",
async: false,
url: webMethod,
data: JSON.stringify({
CSSFile: CSSFile
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {},
error: function(e) {
alert("Error = " + e.responseText);
},
complete: function(msg) {
alert(msg);
if (JSON.parse(msg.responseText).d.toString() != "" || JSON.parse(msg.responseText).d.toString() != null) {
alert("msg.responseText = " + msg.responseText);
$('#ctl00_BodyContent_elm2').val(msg.responseText);
} else if (JSON.parse(msg.responseText).Message.toString() == "" || JSON.parse(msg.responseText).d.toString() == null) {
alert("YOU LOSE!");
} else {
alert("TRY AGAIN");
}
}
});
}
有人可以帮忙吗?
最佳答案
找到解决方案!
complete: function (msg) {
alert(msg);
if (JSON.parse(msg.responseText).d.toString() != "" || JSON.parse(msg.responseText).d.toString() != null) {
alert("msg.responseText = " + msg.responseText);
$('#ctl00_BodyContent_elm2').val(msg.responseText);
}
else if (JSON.parse(msg.responseText).Message.toString() == "" || JSON.parse(msg.responseText).d.toString() == null) {
alert("YOU LOSE!");
}
else {
alert("TRY AGAIN");
}
}
在我的原始代码中,我能够更改传递给我的 elm2 的 val()。代码更改如下:
complete: function (msg) {
alert(msg);
if (JSON.parse(msg.responseText).d.toString() != "" || JSON.parse(msg.responseText).d.toString() != null) {
$('#ctl00_BodyContent_elm2').val(JSON.parse(msg.responseText).d.toString());
}
else if (JSON.parse(msg.responseText).Message.toString() == "" || JSON.parse(msg.responseText).d.toString() == null) {
alert("Nothing in CSS file.");
}
else {
}
}
现在所有内容都按应有的方式显示:)感谢大家的帮助!
关于jquery - 如何以不带特殊字符的字符串形式返回 JSON 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30436135/