6

I have this code (below)..

$.ajax
({
    type: "POST",
    url: "../WebServices/Feedback.svc/sendfeedback",
    dataType: 'json',
    async: false,
    data: '{"stars": "' + stars + '", "rating" : "' + rating + '", "note" : "' + encodeURIComponent(note) + '", "code" : "' + code + '", "permission" : "' + permission + '"}',
    contentType: "application/json; charset=utf-8"
});

I am using this to pass in data to a web service but the problem is if there are any characters in there like this (, / ? : @ & = + $ #). I have put in an encodeURIComponent which works fine and then in the web service I put them back again.

What i'm asking is if there is a better way of accomplishing this? It seems a bit crazy that I have to encode the string each time before passing it through..

Thanks

5
  • why does it seem crazy? isn't it normal to transfer data safely? Commented Mar 10, 2011 at 17:58
  • So what you're saying is these characters are not safe to pass through to a web service? What damage might they do? Commented Mar 10, 2011 at 18:01
  • 2
    $.ajax is going to do the encoding automatically anyway, isn't it? Commented Mar 10, 2011 at 18:05
  • they are not harmful to a web service, but they are harmful inside a JS object. i thought you were using GET, but i see now that you're using POST. Commented Mar 10, 2011 at 18:13
  • Pekka, you are right - it does encode them automatically and i've just tried that.. it works. Thanks :) Commented Mar 10, 2011 at 18:13

2 Answers 2

13

pass data thru as an object instead of a string:

$.ajax
({
...
data: {stars: stars, rating: rating...(etc)}
});
Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me. If I used a query string for data and there was a + in it, it would become a space when my servlet retrieved it, even though I hadn't changed the contentType so it should have encoded it. When I change to this method above, my servlet sees the + correctly in the data.
4

Is the web service belong to you or do you use someone else's web service? What was the reason the web service is not accepting (, / ? : @ & = + $ #)?

jQuery $.ajax default contentType is application/x-www-form-urlencoded which mean jQuery will encode the content. However, since you have specify different contentType, the data is not encoded thus you have to do your own encoding.

Alternatively, you could try to remove the contentType option and pass in your content normally (without encodeURICompnent).

$.ajax
({
    type: "POST",
    url: "../WebServices/Feedback.svc/sendfeedback",
    dataType: 'json',
    async: false,
    data: '{"stars": "' + stars + '", "rating" : "' + rating + '", "note" : "' + note + '", "code" : "' + code + '", "permission" : "' + permission + '"}',
});

3 Comments

I've just tested it again and it doesn't seem to work.. I have tried exactly the above code (without the contentType) and passed in a string containing the characters (, / ? : @ & = + $ #) and still no joy. Any thoughts?
The problem is now not the special characters but newline or returns. If I put a return line in the text it doesn't post to the web service. Is there a quick fix for this?
So you fixed the special characters, good. Without seeing the code I can't be so sure what cause it not posting to your web service. Try this, activate Net Panel (with Firebug, under tab Net) and make the ajax request, see what is error code the server return.

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.