我试图通过使用ES6承诺的jQuery发布帖子请求:
我有一个功能:
getPostPromise(something, anotherthing) {
return new Promise(function(resolve, reject) {
$.ajax({
url: someURL,
type: 'post',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(
something: something,
anotherthing: anotherthing
}),
dataType: 'json',
success: resolve,
error: reject
});
});
}
Run Code Online (Sandbox Code Playgroud)
我称之为:
getPostPromise(
'someFooStuff',
'someBarStuff'
).then(
function(returnedData) {
console.log("Good: ", returnedData);
},
function(responseObject) {
console.log("Bad: ", responseObject);
}
).catch(
function(errorThrown) {
console.log("Exception: ", errorThrown);
}
);
Run Code Online (Sandbox Code Playgroud)
我的服务器正在返回预期的响应,请求主体是JSON格式,但我的控制台输出是:
好:未定义
为什么我没有收到返回的数据?
感谢任何人/每个人的帮助.
---更新编辑---
我把我的js减少到了:
import $ from 'jquery';
$.get('http://localhost:8008/api/user')
.done(function(data) {
console.log(data);
});
Run Code Online (Sandbox Code Playgroud)
我仍然未定义为输出.如果我在网络选项卡中打开请求,我可以看到具有正确数据的响应对象.请求发出,我的服务器很满意并且响应,结果在我的浏览器中,但是done的数据参数是未定义的.我很难过.
---更新2 - 解决方案---
我发现问题在于使用:https …
我习惯在我的对象周围放置标题守卫,如:
#ifndef SOMETHING_H
#define SOMETHING_H
class Something {
...
}
#endif
Run Code Online (Sandbox Code Playgroud)
但我也得到了他们也做的代码:
#ifndef SOMETHING_H
#include "something.h"
#endif
Run Code Online (Sandbox Code Playgroud)
对于每个包括.据说,这更好.为什么?围绕物体的守卫是多余的吗?
我的背景和直觉告诉我,我应该总是通过要求这样的参数创建一个更紧凑和更明确的函数接口:
bool someFunc(int param1, int param2, char param3, float param4) {
...
}
Run Code Online (Sandbox Code Playgroud)
或要求一个对象(结构或类),如:
class someObject {
...
int p1;
int p2;
char c1;
float p4;
}
Run Code Online (Sandbox Code Playgroud)
老板告诉我,我应该使用类似的东西:
bool someFunc(void *params[], int size) {
...
}
Run Code Online (Sandbox Code Playgroud)
因为它创建了更多可扩展(您可以通过这种方式迭代参数)和更快的代码.
我只对提高自己的能力感兴趣,但我的直觉违背了这种方法.他是对的吗?