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?

4
  • Suggest you make these images a bit larger. Hard to read. Commented May 25, 2015 at 10:31
  • @kaz hopefully that will be a bit clearer Commented May 25, 2015 at 10:39
  • Parse this JSON using JSON.parse(that_text) then grab parsed_json.d. Post your code. Commented May 25, 2015 at 10:41
  • @SalmanA it was this answer that provided me with the information to answer my own question. If you want to give a more detailed answer I will mark as "The Accepted Answer"? Commented May 25, 2015 at 11:17

3 Answers 3

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!

Sign up to request clarification or add additional context in comments.

Comments

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>');

2 Comments

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
you may need to replace the \n with <br/> to show it in a HTML page. json_text.replace(/\n/gi, '<br>');
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?

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.