发布 json 问题 ajax
Posting json with ajax issue
我有以下代码,它总是生成 404 错误(未找到):
data = Object {a: "500000", b: "4"}
postJson(data);
function postJson(data){
$.ajax({
url: '/url/postJson',
type: 'POST',
data: data, //also tried "JSON.stringify(data)"
dataType: "json",
contentType: "application/json",
success: function (data, textStatus, jqXHR) {
},
error: function (jqXHR, textStatus, errorThrown) {
alert('error')
}
});
}
在服务器端:
@cherrypy.expose
def postJson(self, data):
print data //just for the test
可能是什么问题?
404 是服务器端的状态。
要检查是否存在并访问您的 url,请尝试 curl:
curl -X POST -H "Content-Type: application/json" --data '{a: "500000", b: "4"}' http://url/portJson
我经常使用的POST Django后端的100%工作函数(我没有设置'contentType'和'dataType',但我设置了'X-CSRFToken'):
function sendAjaxRequest(url, data, done, always, fail, reqType, timeout) {
var _done = function() {};
var _always = function() {};
var _fail = function(jqXHR,status,err){
alert('Error (' + status + '): ' + err + '\n' + jqXHR.responseText);
}
var _reqType ='POST'
var _timeout = 60000;
if (typeof done !== 'undefined') _done = done;
if (typeof always !== 'undefined') _always = always;
if (typeof fail !== 'undefined') _fail = fail;
if (typeof reqType !== 'undefined') _reqType = reqType;
if (typeof timeout !== 'undefined') _timeout = timeout;
var csrftoken = $.cookie('csrftoken');
$.ajax({
url: url,
headers:{ 'X-CSRFToken': csrftoken },
data: data,
cache: false,
type: _reqType,
timeout: _timeout
}).done(_done).always(_always).fail(_fail);
};
使用:
var data = {'a': "5000", 'b': "4"};
sendAjaxRequest(
"/my/url/",
data,
function(response, textStatus, jqXHR) {// done
// do work
}, undefined, undefined, 'POST'
);
根据 this answer 中的代码,您的 JSON 格式似乎需要更新。试试这个。
data = {data: {a: "500000", b: "4"}}
在服务器上更改:
@cherrypy.expose
def postJson(self, data):
print data //just for the test
至:
@cherrypy.expose
@cherrypy.tools.json_in
def postJson(self):
data = cherrypy.request.json
print data //just for the test
我有以下代码,它总是生成 404 错误(未找到):
data = Object {a: "500000", b: "4"}
postJson(data);
function postJson(data){
$.ajax({
url: '/url/postJson',
type: 'POST',
data: data, //also tried "JSON.stringify(data)"
dataType: "json",
contentType: "application/json",
success: function (data, textStatus, jqXHR) {
},
error: function (jqXHR, textStatus, errorThrown) {
alert('error')
}
});
}
在服务器端:
@cherrypy.expose
def postJson(self, data):
print data //just for the test
可能是什么问题?
404 是服务器端的状态。 要检查是否存在并访问您的 url,请尝试 curl:
curl -X POST -H "Content-Type: application/json" --data '{a: "500000", b: "4"}' http://url/portJson
我经常使用的POST Django后端的100%工作函数(我没有设置'contentType'和'dataType',但我设置了'X-CSRFToken'):
function sendAjaxRequest(url, data, done, always, fail, reqType, timeout) {
var _done = function() {};
var _always = function() {};
var _fail = function(jqXHR,status,err){
alert('Error (' + status + '): ' + err + '\n' + jqXHR.responseText);
}
var _reqType ='POST'
var _timeout = 60000;
if (typeof done !== 'undefined') _done = done;
if (typeof always !== 'undefined') _always = always;
if (typeof fail !== 'undefined') _fail = fail;
if (typeof reqType !== 'undefined') _reqType = reqType;
if (typeof timeout !== 'undefined') _timeout = timeout;
var csrftoken = $.cookie('csrftoken');
$.ajax({
url: url,
headers:{ 'X-CSRFToken': csrftoken },
data: data,
cache: false,
type: _reqType,
timeout: _timeout
}).done(_done).always(_always).fail(_fail);
};
使用:
var data = {'a': "5000", 'b': "4"};
sendAjaxRequest(
"/my/url/",
data,
function(response, textStatus, jqXHR) {// done
// do work
}, undefined, undefined, 'POST'
);
根据 this answer 中的代码,您的 JSON 格式似乎需要更新。试试这个。
data = {data: {a: "500000", b: "4"}}
在服务器上更改:
@cherrypy.expose
def postJson(self, data):
print data //just for the test
至:
@cherrypy.expose
@cherrypy.tools.json_in
def postJson(self):
data = cherrypy.request.json
print data //just for the test