代码引发异常'未定义[specific_method];
Codes raises exception '[specific_method] is not defined'
我有以下javascript代码。它只是一个类,应该从RESTWCF客户端接收一些数据。
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');
}
}
我不知道为什么它会引发异常:"doGetRequest未定义"。有人能帮忙吗?
答案很简单:在"return this.doGetRequest('employes');"中使用此运算符。在第一个代码示例中,缺少this运算符。
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
。最好使用回调或承诺。
相关文章:
- document.applet.method在Mounatin Lion上抛出safari 6+JDK7异常
- 当method=post时,jQueryAjax返回404
- 调用laravel{{action(Controller@method}}通过传递vue.js数组中的变量
- Java eqivalent method of “splice(a,b,..)“在 JavaScript 方法中
- JavaScript Loop to setData method
- Extending jQuery.fn.method
- Javascript - jQuery preventDefault method
- 如何显示Ajax Get-Method中的数据
- Javascript method win.validate();
- fine uploader uploadStoredFiles method
- jQuery deferred method and ajax beforeSend()
- 调用Action Method,关闭对话框并刷新父项
- JavaScript "Object Method"
- JavaScript Post Method
- 方法不允许 — 从 Angularjs 中的请求标头中删除 Access-Control-Request-Method
- 理解ReactJS中的.method.bind(null,..)
- JavaScript, Page Method, and Gridview
- 从Meteor.method中的回调返回值
- NodeJS - jQuery DELETE Method
- Apache-specific md5 javascript impelementation for .htpsswrd