HTTP Post 请求未成功执行或出错
HTTP Post request not executing to success or error
我在 Cloud Code 中处理我的 HTTP post 请求时遇到问题,由于某种原因,它实际上没有收到成功或错误消息。它将注销我编码的 JSON 变量,但似乎根本不执行 HTTP 请求。关于我的问题有什么想法吗?
Parse.Cloud.job("getUsers", function(request, status) {
// Query to get users
var users = Parse.Object.extend("_User");
var query = new Parse.Query(users);
var t = 0;
query.notEqualTo("mkUsername", "");
query.notEqualTo("mkPassword", "");
query.equalTo("canUseMK", "1");
query.find({
success: function(results) {
for (var i = 0; i < results.length; ++i) {
var object = results[i];
var requestVariables = { mkUsername: object.get("mkUsername"), mkPassword:object.get("mkPassword"), userID: object.id };
var requestJsonString = JSON.stringify(requestVariables);
console.log(requestJsonString);
Parse.Cloud.httpRequest({
method: 'POST',
url: 'http://url.com/api_trial.php',
headers: {
'Content-Type': 'application/json;charset=utf-8',
'authKey' : 'XXXXXXXXXX'
},
body:requestJsonString,
success: function(httpResponse) {
++t;
console.log(httpResponse.text);
response.success(httpResponse.text);
status.message("User "+ i + " Response " + httpResponse.status);
},
error: function(httpResponse) {
//get the status code
if (httpResponse.status == 400) {
console.error('Request failed with response code ' + httpResponse.status);
response.error('Request failed with response code ' + httpResponse.status);
}
else if (httpResponse.status == 401) {
console.error('Request failed with response code ' + httpResponse.status);
response.error('Request failed with response code ' + httpResponse.status);
}
else if (httpResponse.status == 409) {
console.error('Request failed with response code ' + httpResponse.status);
response.error('Request failed with response code ' + httpResponse.status);
} else {
console.error('Request failed with response code ' + httpResponse.status);
response.error('Request failed with response code ' + httpResponse.status);
}
}
});
}
status.success("Synced "+ i + " Users Data");
},
error: function(error) {
status.error("Had query issue: " + error);
}
});
});
HTTP 请求异步执行,但您在启动可能尚未完成的 HTTP 请求后直接调用 status.success
。
这将使作业 return 如同已完成,并且任何 运行 HTTP 请求都应终止,从而导致您看到的行为。将 status.success
的调用移动到您请求的成功函数中,应该没问题。
我在 Cloud Code 中处理我的 HTTP post 请求时遇到问题,由于某种原因,它实际上没有收到成功或错误消息。它将注销我编码的 JSON 变量,但似乎根本不执行 HTTP 请求。关于我的问题有什么想法吗?
Parse.Cloud.job("getUsers", function(request, status) {
// Query to get users
var users = Parse.Object.extend("_User");
var query = new Parse.Query(users);
var t = 0;
query.notEqualTo("mkUsername", "");
query.notEqualTo("mkPassword", "");
query.equalTo("canUseMK", "1");
query.find({
success: function(results) {
for (var i = 0; i < results.length; ++i) {
var object = results[i];
var requestVariables = { mkUsername: object.get("mkUsername"), mkPassword:object.get("mkPassword"), userID: object.id };
var requestJsonString = JSON.stringify(requestVariables);
console.log(requestJsonString);
Parse.Cloud.httpRequest({
method: 'POST',
url: 'http://url.com/api_trial.php',
headers: {
'Content-Type': 'application/json;charset=utf-8',
'authKey' : 'XXXXXXXXXX'
},
body:requestJsonString,
success: function(httpResponse) {
++t;
console.log(httpResponse.text);
response.success(httpResponse.text);
status.message("User "+ i + " Response " + httpResponse.status);
},
error: function(httpResponse) {
//get the status code
if (httpResponse.status == 400) {
console.error('Request failed with response code ' + httpResponse.status);
response.error('Request failed with response code ' + httpResponse.status);
}
else if (httpResponse.status == 401) {
console.error('Request failed with response code ' + httpResponse.status);
response.error('Request failed with response code ' + httpResponse.status);
}
else if (httpResponse.status == 409) {
console.error('Request failed with response code ' + httpResponse.status);
response.error('Request failed with response code ' + httpResponse.status);
} else {
console.error('Request failed with response code ' + httpResponse.status);
response.error('Request failed with response code ' + httpResponse.status);
}
}
});
}
status.success("Synced "+ i + " Users Data");
},
error: function(error) {
status.error("Had query issue: " + error);
}
});
});
HTTP 请求异步执行,但您在启动可能尚未完成的 HTTP 请求后直接调用 status.success
。
这将使作业 return 如同已完成,并且任何 运行 HTTP 请求都应终止,从而导致您看到的行为。将 status.success
的调用移动到您请求的成功函数中,应该没问题。