jQuery的AJAX编码数据数据、jQuery、AJAX

2023-09-10 13:45:45 作者:倾城之夏

我有这个code(下图)。

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"
});

我使用这个数据传递到Web服务,但问题是,如果里面还有这样的任何字符(,/?:@&安培; = + $#)。我已经把在一间codeURIComponent工作正常,然后在Web服务,我把它们放回去了。

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.

如果没有完成这个更好的方法就是我要问的?这似乎有点疯狂,通过传递之前,每一次,我都不得不EN code中的字符串。

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..

感谢

推荐答案

是Web服务属于你或你使用别人的网络服务?什么是Web服务不接受的原因是什么?(,/?:@&安培; = + $#)

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的 $。阿贾克斯默认的contentType是应用程序/ x-WWW的形式urlen codeD 这意味着jQuery将连接code的内容。不过,既然你有不同的指定的contentType,数据不连接codeD所以你必须做你自己的编码。

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.

另外,你可以尝试删除的contentType 选项,并通过在您的内容(通常不连接codeURICompnent )。

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 + '"}',
});