NullException
NullException

Reputation: 4510

jQuery ajax POST json

So, here is my issue ( I did study and try to fix my issue by looking at other similar issue posted here but did not work )

Snippet of code (Ignore braces and stuff):

json_string = JSON.stringify(json_links); 
var data_obj = { id:n_id, links_json: json_string }; 

$.ajax({ 
url: 'server_api', 
type: 'GET', 
data: data_obj, 
dataType: 'json',

On server side, I try to decode using perl module, JSON and function, decode_json but it throws an exception, 'malformed UTF-8 character in JSON string, at character offset 48 (before "\x{92f}does-it-...") '

I tried using POST method & content-type combination but it didn't work. jQuery documentation specify that it encodes to UTF-8 by default? Please help?

Upvotes: 1

Views: 17212

Answers (2)

taswyn
taswyn

Reputation: 4513

You are probably passing encoded Unicode characters to decode_json, not binary UTF-8 characters. See here for the issue and two easy potential fixes.

Some other things are that you aren't escaping entity names (I assume those are meant to be entity names in and of themselves, given their associated values) in your data_obj. Instead it is trying to insert the contents of those variables. I'm assuming you wanted those to be the identifiers themselves:

data_obj = { 'id' : n_id, 'links_json' : json_string }

Unless you are simply accessing data already stored on the server using this, you should consider using POST (or PUT) instead, to be more properly RESTful.

Also, GET is performed by appending to the URL, and you need to be aware of:

dataObject, String

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing.

processData, Boolean

Default: true

By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

Because the data is URL appended, even if your javascript is trying to send properly binary encoded UTF-8, it may still get translated into encoded Unicode.

Further, datatype is irrelevant for the data you are sending and refers to what data you expect to receive back. I assume you realize this but I wanted to be sure.

So you may want to stringify your data_obj yourself rather than let .ajax do it for you.

Have you tried simply echoing out your received (server side) data raw (without running a json decode) to see what you're receiving server side, and whether it matches what you expected? It's hard to tell what your problem is necessarily without a sample dataset that replicates it for you. I'm assuming it's going to be related to Perl's json_decode and related encoding issues (the first line here), however.

Upvotes: 1

Jason Whitted
Jason Whitted

Reputation: 4024

Try passing the data as stringified JSON.

$.ajax({
    url: 'server_api',
    type: 'GET',
    data: JSON.stringify(data_obj),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json'
});

Upvotes: 6

Related Questions