0

i am stuck with this for quiet sometime now. all i am trying to do is send a json text to php, process it and send a response bacl..

here is the data i am sending as my textarea value..

**asdasd #$**&**%^*( AAAA SADQWEASD /// '' \\\ '' l; "" **

below is the json sent to php (got it from console):

data={"Text":"asdasd #$&%^*( AAAA SADQWEASD /// '' \\\\\\ '' l; \"\" "}

i am using jquery ajax function as below:

    function ajaxfunc(data, path){
        $.ajax({
            type: "POST",
            url: path, 
            data: 'data='+data, 
            success: function(response){
            }
        });
    }

in php, i am doing this.

$res = json_decode(stripslashes(escapeJsonString($_POST['feed'])), true);

function escapeJsonString($value) { 
    # list from www.json.org: (\b backspace, \f formfeed)

    $search = array("\n", "\r", "\u", "\t", "\f", "\b", "/", '"');
    $replace = array("\\n", "\\r", "\\u", "\\t", "\\f", "\\b", "\/", "\"");
    $result = str_replace($search, $replace, $value);

    $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c"); 
    $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b"); 
    $result = str_replace($escapers, $replacements, $value); 

    return $result; 
}

echo $res["Text"];

issue is : & .. is not getting parsed at php and hence the response is null.. i also wanted to make sure, the new line characters are detected.. basically i am expecting "WYSIWYG" using json.. json_encode and json_decode

Andrew
  • 14,325
  • 4
  • 43
  • 64
Kannan Lg
  • 911
  • 4
  • 11
  • 21
  • 1
    Maybe this is a typo or an obfuscation failure, but `$_POST['feed']` is not going to contain your data, since you sent it with the key `data` in the `$.ajax` call. – Andrew Dec 01 '11 at 04:49
  • @Andrew : yes, its a typo.. it should be $_POST['data'] – Kannan Lg Dec 01 '11 at 05:00
  • There may be better alternatives to what you are trying to do. Why do you need to send the data in json format back to the server? If you are just wanting to access the data on the server as key => value pairs then you can pass a JS object to the $.ajax function and that will encode them as a query string and POST them to the server. Can you clarify what you want to do here? – Simon Dec 01 '11 at 05:05
  • wat i am doing is using serializeArray() to get form values and sending it to php using JSON.Stringify.. pls suggest an alternative.. – Kannan Lg Dec 01 '11 at 05:21
  • If you have a set of form inputs then you're after serialize(), not serializeArray(). serializeArray() is useful for processing the form inputs inside your JS but if you want to just send it to the server, user serialize(). – Simon Dec 01 '11 at 05:25
  • I just noticed that you are passing the "data" property of the $.ajax function parameter as a string concatenated with an object ie. 'data='+{}. $.ajax expects either a string or an object. If you pass in an object it will automatically create the query string. I suspect all you want is the json string so use the JSON-js lib to create the string, then concatenate it with 'data=' and then pass it to $.ajax. – Simon Dec 01 '11 at 05:45

2 Answers2

1

Based on the comment about just wanting to send form inputs to the server, use serialize() in the following way:

$.ajax({
    type: "POST",
    url: path, 
    data: $('form').serialize(), 
    success: function(response){
    }
);

If all you want to do is access the form inputs on the server through the $_POST array then converting to JSON on the client is a waste of effort. serialize() should work perfectly for you.

But if you really want to use a JSON then use the JSON-js lib by Douglas Crockford here. Note that there is a lot of other info on this topic: Serializing to JSON in jQuery. Example below:

$.ajax({
    type: "POST",
    url: path, 
    data: 'data='+( JSON.stringify(data) ), 
    success: function(response){
    }
);
Community
  • 1
  • 1
Simon
  • 1,756
  • 12
  • 8
  • the reason y i am avoiding serialize() or serializeArray() is it will send data as below: {"name" : "data"}, {"value" : "asdasdas asd"} instead of {"data" : "asdasdas asd"} i am trying to send POST data to PHP as above.. i also wanted to retain new line characters within – Kannan Lg Dec 01 '11 at 05:34
0

if you want to send a data in JSON format, you can try this

data =  {"Text":"asdasd #$&%^*( AAAA SADQWEASD /// '' \\\\\\ '' l; \"\" "};
data =  JSON.stringify(data);

then send with your jquery.ajax or jquery.post

manny
  • 1,878
  • 2
  • 15
  • 31