我通过jQuery向服务器发送消息,当它到达Python部分时,+#被添加到我的字符串中:(
的jQuery
// RESPONSE BUTTON:
$('.friend_response').unbind('click').bind('click', function () {
var choice = $(this).siblings('p').text().toLowerCase(),
theBtn = $(this),
id = $('.friend_id_details').data('friend-id'),
message = "I would like to add you to my friend network.";
if (choice === 'accept') {
$(theBtn).removeClass('big_accept');
$(theBtn).addClass('big_accept_clicked');
$('.big_deny').attr("disabled", "disabled");
$('.big_deny').css('cursor','auto');
$('.button_accept p').text('Accepting...');
sendFriendResponse(message_id, message, id, '/accept');
} else if (choice === 'deny') {
$(theBtn).removeClass('big_deny');
$(theBtn).addClass('big_deny_clicked');
$('.big_accept').attr("disabled", "disabled");
$('.big_accept').css('cursor','auto');
$('.button_deny p').text('Denying...');
sendFriendResponse(message_id, ' ', id, '/deny');
}
});
^上面我的消息只是"I would like to add you to my friend network."
我的postToServerWithAjax看起来像:
$.ajax({
type: postMethod,
url: url,
cache: false,
// data: JSON.stringify(params),
data: params,
dataType: 'JSON',
statusCode: {
200: function (data, textStatus, jqXHR) {
//console.log('200');
callback(data);
},
201: function (data, textStatus, jqXHR) {
//console.log('201');
callback(data);
},
400: function (data, textStatus, jqXHR) {
//console.log('400');
callback(data);
}
}
});
params对象:
Object {message: "I would like to add you to my friend network.", id: 6}
最后在Python中:
@view_config(route_name="accept:request", request_method='POST')
def accept_request(self):
success_msg = ["You accepted the request"]
error_msgs = ["Bacon is missing, we're looking into the issue"]
try:
json = self.body
id = int(json['id'])
message = str(json['message'])
print id
print message
self.ctx.messaging.accept(id, message)
value = {'result': 'success', 'message': random.choice(success_msg)}
except Exception, err:
value = {'result': 'error', 'message': random.choice(error_msgs)}
print err
return self.emit_json(value)
印刷声明:(
6
I+would+like+to+add+you+to+my+friend+network.
我也尝试了json['message']
,但结果相同
答案 0 :(得分:4)
您没有发送JSON;您正在发送常规application/x-www-form-urlencoded
请求。 X-WWW-Form-URLEncoded POST主体使用URL percent encoding;空格在该编码中被编码为+
个字符。
jQuery这样做是因为data
不是字符串。您必须自己将数据编码为JSON:
$.ajax({
type: postMethod,
url: url,
cache: false,
data: JSON.stringify({'params': params}),
contentType: "application/json",
dataType: 'JSON',
statusCode: {
200: function (data, textStatus, jqXHR) {
//console.log('200');
callback(data);
},
201: function (data, textStatus, jqXHR) {
//console.log('201');
callback(data);
},
400: function (data, textStatus, jqXHR) {
//console.log('400');
callback(data);
}
}
});
为您发送的数据设置正确的内容类型总是一个好主意。
设置dataType
参数仅对响应有用,告诉jQuery
将收到的数据解码为JSON。它与data
如何发送到服务器无关。
看起来好像你在Python端使用Pyramid;您希望使用Request.json_body
作为Python对象访问JSON编码的数据。
答案 1 :(得分:1)
不确定发生了什么,但又遇到了同样的问题......
首先尝试使用Python
for char in title:
if char in "+":
cleaned_title = title.replace(char, ' ')
第二次尝试,更好的jQuery&蟒强>
titleVar = $('#the-title').val();
if (titleVar.indexOf(' ')) {
var titleCleaned = titleVar.split(' ').join('%20');
}
然后在Python中:
import urllib2
if 'params' in self.body:
params = self.body['params']
pars = urllib2.unquote(params)
cleaned = json.loads(pars)
^然后做一个for循环......或尝试修复下面的垃圾好多了:
无法弄清楚jQuery Ajax调用的确切完美设置是什么:
var invokeServerWithJSON = function(url, params, callback, postMethod) {
WHOAT.analytics.trackPageView(url);
$.ajax({
type: postMethod,
url: url,
cache: false,
data: {'params': JSON.stringify(params)},
// contentType: "application/json",
// success: callback,
// dataType: 'JSON',
statusCode: {
200: function (data, textStatus, jqXHR) {
callback(data);
},
201: function (data, textStatus, jqXHR) {
callback(data);
},
400: function (data, textStatus, jqXHR) {
callback(data);
}
}
});
}