2

On an HTMl i have a textarea (a comment box) (id = #comment") that can contain multiple lines. When I send it through with ajax i have no problems. Although when retrieve this data, the JSON-data is no longer valid(below a minor extract of json:

"items" : [  {  
     ...<<other values>>...,
     "comment": "Approved!!!!

     Or Not

     Or Did I


     Approve this....." 
     ,...<<other values>>...}

So i assumed I'd had to convert the value of my textarea when sending it to the server. But when i tried using JSON.Parse I got an error:

$.ajax({
    type: "POST",
    url: "updateitem.aspx",
    data: {
        myId: $("body").attr("id"),
        comment: JSON.parse($("#comment").val()),
    }
    ...
})

But this results an error:

SyntaxError: JSON.parse: unexpected keyword at line 1 column 1 of the JSON data
comment: JSON.parse($("#comment").val()),

So my question really is: How can send the data from an textarea to the server. While preserving any non-JSON characters & linebreaks. The solution is probably simple but the only this i seem to find are: Past JSON in textarea, ReplaceAll (which could work but i'm searching for a better solution)

Tried the following solution but to no avail. how-can-i-pass-textarea-data-via-ajax-that-contains-ampersands-and-or-linebreaks

Chandan
  • 11,465
  • 1
  • 6
  • 25
User999999
  • 2,500
  • 7
  • 37
  • 63
  • ... why arent you just doing `comment:$("#comment").val()`, your sample doesn't show your comments being made up of JSON text – Patrick Evans Aug 20 '14 at 08:32
  • JSON.parse, takes a JSON string like: `'{"somename":"somevalue"}'` and turns it into a javascript object – Patrick Evans Aug 20 '14 at 08:33
  • "The JSON.parse() method parses a string as JSON, optionally transforming the value produced by parsing." from [link]The JSON.parse() method parses a string as JSON, optionally transforming the value produced by parsing.[link]. So i hope it would transform the `val()`to valid jSON. But apparenty i totally was wrong here... – User999999 Aug 20 '14 at 08:34
  • 1
    Possible duplicate: http://stackoverflow.com/questions/42068/how-do-i-handle-newlines-in-json – mrcrgl Aug 20 '14 at 08:35
  • _"Although when retrieve this data, the JSON-data is no longer valid(below a minor extract of json:"_ how are you retrieving the data, and are you JSON encoding it first? For instance in PHP you `echo json_encode($someData);` not sure of the asp equivalent is – Patrick Evans Aug 20 '14 at 08:40
  • No encode yet. Can't seem to find the right function to do that. @Marc Not a duplicate as my `$("#comment").val()` does not contain `\n` (at least not when i'm debugging it) – User999999 Aug 20 '14 at 08:41
  • @N55PEC I'm really interested how to represent a line break in plain text without a newline operator `\n` – mrcrgl Aug 20 '14 at 09:15
  • @Marc. I'm using Firebug to debug it and at the point i'm getting my `$("#comment").val()` I don't see any `\n`. This could be due to the fact of faulty Firebug configuration. But then I would expect the `\n` to occur in our database(after inserting it). Which it doesn't. So that still raises the questions how the linebreaks are actually encoded/coded. I'm still searching for the why/how of this (which is probably very simple, I just haven't found it yet) – User999999 Aug 20 '14 at 09:21
  • Just assume, every time you see in plain text a line break, it will be represented by \n. Give it a try and just do: `yourFunkyLineBreakText.replace("\n", "[NL]");` (important: use double qoutes) – mrcrgl Aug 20 '14 at 09:25
  • @Marc depending on the situation a `linebreak`might be `\n`. Unfortunatly in this case it wasn't, it seems that EMC using alternative means to store linebreaks. – User999999 Aug 20 '14 at 11:28

1 Answers1

1

You need use JSON.stringify for this:

var comment = JSON.stringify($("#comment").val());
        
// Now comment is a JSON string to send to your server

$.ajax({
    type: "POST",
    url: "updateitem.aspx",
    data: {
        myId: $("body").attr("id"),
        comment: comment,
    }
    ...
})

See the jsfiddle: http://jsfiddle.net/14cjojhq/1/

Chandan
  • 11,465
  • 1
  • 6
  • 25
SnakeDrak
  • 3,406
  • 4
  • 28
  • 41
  • Just the function i needed to find. Thanx Man! – User999999 Aug 20 '14 at 08:46
  • @Patrick Evans No. `JSON.stringify` converts to JSON. See [this jsfiddle](http://jsfiddle.net/14cjojhq/2/). The `$('#comment').val()` not return a valid JSON (break-line, etc). **UPDATED**: Patric Evans has removed his comment. – SnakeDrak Aug 20 '14 at 08:52