import * as React from "react";
import $ = require("jquery");
export class Hello extends React.Component<{}, {}> {
public delete() {
console.log("delete");
$.ajax({
type: "DELETE",
url: "http://127.0.0.1:8080/delete",
xhrFields: {withCredentials: true}
}).done((data: any): void => {
console.log("delete data:" + data);
}).fail((jqXHR: any): void => {
console.log("delete faildata: " + jqXHR.status);
});
}
public save() {
console.log("save");
$.ajax({
type: "POST",
url: "http://127.0.0.1:8080/save",
dataType: "text",
crossDomain: true,
xhrFields: {withCredentials: true}
}).done((data: any): void => {
console.log("data:" + data);
{this.delete()}
}).fail((jqXHR: any): void => {
console.log(jqXHR);
console.log("faildata: " + jqXHR.status);
});
}
render() {
return (
<div>
<button onClick={this.delete}>delete</button>
<button onClick={this.save}>save</button>
</div>
);
}
}
更新代码,如果操作完成,请单击“保存”。开始删除数据, 但现在看来是错误的 enter image description here Hello.tsx:36未捕获TypeError:无法读取属性&#39;删除&#39;未定义的,我已经看到了TypeScript calling methods on class inside jquery function scope的例子,我不知道为什么可以运行?
答案 0 :(得分:0)
尝试
private save() {
console.log("save");
$.ajax({
type: "POST",
url: "http://127.0.0.1:8080/save",
dataType: "json",
crossDomain: true,
xhrFields: {withCredentials: true}
}).done((function (data) {
console.log("data:" + data);
this.delete();
}).bind(this)).fail((function (jqXHR:any) {
console.log("faildata: "+jqXHR.status)
this.delete();
}).bind(this));
}