2

I am looking to return a files contents and I am using a webservice and JSON to do this.

I have managed to return the contents but it is not in the format I want it to be in.

I need the top text area to be in the same format as the textarea on the bottom:

enter image description here

enter image description here

I need to to display the \n as a new line rather than just remove it from the string.

Current code (ignore alerts, only for my own testing purposes):

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");
            }
        }
    });
}

Can anyone help?

Salman A
  • 262,204
  • 82
  • 430
  • 521
Paul C
  • 153
  • 1
  • 3
  • 15

3 Answers3

1

Solution found!

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");
            }
        } 

In my original code I was able to change the val() being passed to my elm2. Code change below:

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 {
                }
            }  

All now displaying as it should :) Thanks for everyones help!

Paul C
  • 153
  • 1
  • 3
  • 15
0

try the solution in javascript regexp remove all special characters

take you json return and replace special characters:

var desired_json = json_text.replace(/[^\w\s]/gi, '');

It could be that you want only to replace new lines:

var desired_json = json_text.replace(/\n/gi, '');

EDIT: to show the special characters as line breaks...

 var desired_json = json_text.replace(/\r\n/gi, '<br>');
Community
  • 1
  • 1
Dror Hilman
  • 6,837
  • 9
  • 39
  • 56
  • unfortunately this didn't work. It replaced all the special characters but I need to to display the \n as a new line rather than just remove it from the string. I should probably add this to the original question to make it a bit clearer as to what I am trying to do – Paul C May 25 '15 at 10:49
  • you may need to replace the \n with
    to show it in a HTML page. json_text.replace(/\n/gi, '
    ');
    – Dror Hilman May 25 '15 at 11:10
0

ignoring the alerts i take that $('#ctl00_BodyContent_elm2').val(msg.responseText); is the actual code to output the results? what kind of element is '#ctl00_BodyContent_elm2'?

try adding some <textarea> in your html and in ajax complete add

$('textarea').val( JSON.parse(json).d );

is that what you're looking for?

  • It was in a textarea but it was how I was parsing the JSON call that was causing the problem. See the marked answer for more info if you wish – Paul C May 25 '15 at 11:18