Jquery ajax编码数据

我有这个代码(下面)..

$.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服务,但问题是如果有任何字符在这里(,/?:@&= + $#)。 我已经放入了一个工作正常的encodeURIComponent,然后在Web服务中我再次将它们放回去。

我问的是,是否有更好的方法来实现这一目标? 似乎有点疯狂,每次传递它之前我必须编码字符串..

谢谢

Web服务是属于您还是您使用其他人的Web服务? Web服务不接受的原因是什么(?/?:@&= + $#)?

jQuery $ .ajax默认的contentType是application / x-www-form-urlencoded ,这意味着jQuery将对内容进行编码。 但是,由于您指定了不同的contentType,因此数据不会被编码,因此您必须进行自己的编码。

或者,您可以尝试删除contentType选项并正常传入您的内容(不使用encodeURICompnent )。

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

将数据作为对象而不是字符串传递:

 $.ajax ({ ... data: {stars: stars, rating: rating...(etc)} });