Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encode as json object and send it via ajax

Tags:

json

jquery

php

I have found this: http://code.google.com/p/jquery-json, a plugin that emulates json_encode PHP function.

So I tried this:

var datasend = {};
    datasend['name'] = $(".chat_username").val();
    datasend['message'] = $(".chat_text").val();
    encoded_send = $.toJSON( datasend );
    $.ajax({
      type: "POST",
      url: "/apps/chat_write",
      data: {send_data: encoded_send },
      success: function(response){
        if(response!="OK"){
            alert("Παρουσιάστηκε πρόβλημα.Κάντε ανανέωση την σελίδα ή προσπαθήστε ξανά αργότερα.");
        }else{
            $(".chat_text").val("");
            $(".chat_count").val("0/100");
        }
      }
    });

The first problem?I have this

    $.ajax({
      type: "POST",
      url: "/apps/chat_write",
      data: "name="+escape($(".chat_username").val())+"&message="+escape($(".chat_text").val()),
      success: function(response){
        if(response!="OK"){
            alert("Παρουσιάστηκε πρόβλημα.Κάντε ανανέωση την σελίδα ή προσπαθήστε ξανά αργότερα.");
        }else{
            $(".chat_text").val("");
            $(".chat_count").val("0/100");
        }
      }
    });
    return false;
});

And i have problems with greek characters.. so i decidied to use json object If this code is correct how can I parse the JSON object from PHP? If it's not correct where are the errors?

Thanks

like image 456
Chris P Avatar asked Mar 23 '26 02:03

Chris P


1 Answers

(before I continue to your function..)
You seem to misunderstand the purpose of JSON. It's not necessary to replace the data with a JSON string when transmitting data to a server. A much easier approach is adding parameters through data, and retrieving them through $_POST.

Example:

$.ajax({
    type: "POST",
    url: "/apps/chat_write",
    data: {
        name: $(".chat_username").val(),
        message: $(".chat_text").val()
    },
    dataType: "json", /* This parameter is ONLY used at the response */
    success: function(response){
        //response is a deserialized JSON string
    }
});

Simple PHP example:

<?php
    header('Content-Type: application/json');
    $example = array("what" => "This");
    echo json_encode($example); //Echos: {"what":"This"}
?>

JSON should be used in a server respons. PHP objects can be serialized using json_encode. Make sure that you use header('Content-Type: application/json'); before sending any output.

(if you still want to JSON-encode data before sending)
Use the json_decode PHP function to deserialize a JSON string.

Your current code has to be fixed though.

var datasend = {};
datasend['name'] = $(".chat_username").val();
datasend['message'] = $(".chat_text").val();
var encoded_send = $.toJSON( datasend );
$.ajax({
  type: "POST",
  url: "/apps/chat_write",
  data: {send_data: encoded_send },
  dataType: "json", /* Can be removed if the RESPONSE is not JSON*/
  success: function(response){
    if(response!="OK"){
        alert("Παρουσιάστηκε πρόβλημα.Κάντε ανανέωση την σελίδα ή προσπαθήστε ξανά αργότερα.");
    }else{
        $(".chat_text").val("");
        $(".chat_count").val("0/100");
    }
  }
});
like image 176
Rob W Avatar answered Mar 24 '26 16:03

Rob W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!