0

My application contains multiple <input type="file"> fields and I need to save the file paths. I am converting the paths into a JSON string and sending the result to a ColdFusion CFC method. On the ColdFusion side I am deserializing the JSON string using DeserializeJSON(), but am getting an error:

JSON Parsing ending with unexpected character

.

I had the same issue when passing a serialized FORM structure into a ColdFusion page. I fixed it by URL encoding the value with URLEncodedFormat(). Would url encoding the above JSON string fix this issue or is my code for passing the file paths as a JSON string incorrect?

My client side code is below:

var lttr_docs = {};
for(i=1;i<length;i++)
{
    lttr_docs['file_id_'+i] = $('#file_id_'+i).val();
}

$.ajax({
    url: "xyz/component.cfc?method=methodName",
    type: "GET",
    dataType:"json",
    data: JSON.stingify(lttr_docs),
    success: function(res)        {
    },
    error: function(jqXHR, textStatus, errorThrown) {
        alert(textStatus);                
    }
});

Basically the problem is while desrializing the JSON string in server side. Can I do some thing on client side to fix this or do I need to completely rely on a server side fix?

1 Answer 1

1

dataType is not for telling the server what kind of data you're sending it, even though data is for the data you're sending; it's just one of the flaws in the jQuery API. Counter-intuitively, dataType is for telling jQuery what kind of data you expect back. So dataType: "json" is probably wrong. Instead, you want contentType: "application/json".

So your GET is getting sent to the server with the default application/x-www-form-urlencoded; charset=UTF-8 content type, which is probably confusing it.

Separately, as Rocket Hazmat points out in a comment below, specifying the content type on a GET is a bit fishy. You probably want to change this to a POST (both in the client code and the server code processing it).

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

2 Comments

Can you use contentType with a GET request?
@RocketHazmat: Good catch, I didn't notice until adding the second paragraph it was a GET. I don't think you can, no, but I don't use GET for sending data to servers so I don't know the ins and outs of it... I'll add a note.

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.