I've got following javascript code. It's simply class, that should receive some data from REST WCF client.
class EmployeesWcfClient {
constructor(url) {
if (url == null) {
url = "http://localhost:35798/MyCompanyService.svc/";
}
this.url = url;
}
doGetRequest(relUrl) {
return $.ajax({
type: 'GET',
contentType: 'json',
dataType: 'json',
url: this.url + relUrl,
async: false
});
}
doPostRequest(relUrl, data) {
return $.ajax({
type: 'POST',
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
url: this.url + relUrl,
async: false
});
}
getEmployees() {
return doGetRequest('Employees');
}
}
And I don't know why it's raises exception: 'doGetRequest is not defined'. Could someone help?
答案 0 :(得分:0)
答案很简单: 在'return this.doGetRequest('Employees');'中使用此运算符。在第一个代码示例中,缺少此运算符。
答案 1 :(得分:-1)
在doGetRequest的ajax中,this.url
不会引用EmployeesWcfClient.url,而是指向ajax选项对象本身。所以在调用ajax请求之前请参考这个。
doGetRequest(relUrl) {
var _this = this;
return $.ajax({
type: 'GET',
contentType: 'json',
dataType: 'json',
url: _this.url + relUrl,
async: false
});
}
但我不确定是否返回此ajax函数将完全返回请求中的响应,尽管您已将其设为async: false
。更好地使用回调或承诺。