以下是两个javascript函数:
function testAjax() {
$word = $('#word').val();
return $.ajax({
url: "http://test.movies.9pstudio.com:8080/wordchain/wordsearch",
type: 'POST',
data: {
word: word
},
});
}
function validation() {
var reply = testAjax();
var x = document.getElementById("word").value;
alert(reply);
if (reply.equals("word not exist")) {
document.getElementById("error").innerHTML = reply;
} else {
document.getElementById("word1").innerHTML = reply;
send.push(result);
}
document.getElementById("word").value = "";
}
答案 0 :(得分:1)
使用jQuery ajax回调
function validation() {
var reply = testAjax();
reply.done(function(data) {
var x = document.getElementById("word").value;
alert(data);
...
})
}
答案 1 :(得分:1)
通用功能:
function ajax(url, data, type, successCallBack, failureCallBack) {
var that = this;
// ajax request to server
$.ajax({
url: url,
type: type,
data: data
}).done(function(data) {
successCallBack(data);
}).fail(function(data) {
failureCallBack(data);
});
};
您可以使用此通用功能在应用程序中的任何位置调用:
function textAjax(){
ajax('test.json','GET',{'word':word},function(reply){
var x = document.getElementById("word").value;
alert(reply);
if (reply.equals("word not exist")) {
document.getElementById("error").innerHTML = reply;
} else {
document.getElementById("word1").innerHTML = reply;
send.push(result);
}
document.getElementById("word").value = "";
},function(data){
// write code for failure handler
});
答案 2 :(得分:0)
我认为您正在尝试使用success
回调函数,如下所示:
function testAjax() {
//this is not php so you need to declare a variable using "var" keyword
var word = $('#word').val();
$.ajax({
url: "http://test.movies.9pstudio.com:8080/wordchain/wordsearch",
type: 'POST',
data: {
word: word
},
success: validation.bind(this)
});
}
function validation(response) {
var reply = response;//testAjax();
var x = document.getElementById("word").value;
alert(reply);
if (reply === "word not exist") {
document.getElementById("error").innerHTML = reply;
} else {
document.getElementById("word1").innerHTML = reply;
//send.push(result); //where did you declare send variable?
}
document.getElementById("word").value = "";
}
答案 3 :(得分:0)
您应该success
执行complete
或function testAjax() {
word = $('#word').val();
return $.ajax({
url: "http://test.movies.9pstudio.com:8080/wordchain/wordsearch",
type: 'POST',
data: {
word: word
},
success: function(data){
// Do something with response data here
}
});
}
处理程序。
示例:
{{1}}
答案 4 :(得分:0)
在第一个函数中使用回调参数
function testAjax(callback) {
$.ajax({
url:...
success: function(data) {
callback(data);
}
});
}