1

Here is the two javascript functions:

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 = "";
}
6
  • 2
    Format your code if you want anyone to help you! Commented Mar 11, 2014 at 8:50
  • Why $word is initialized but not used ever? Commented Mar 11, 2014 at 8:52
  • @DhavalMarthak $word is being sent as post data. Commented Mar 11, 2014 at 8:53
  • @ElmoVanKielmo That's why i pointed out, it's only word not $word. Commented Mar 11, 2014 at 8:53
  • @DhavalMarthak this I believe is a typo only. Commented Mar 11, 2014 at 8:54

5 Answers 5

1

Use jQuery ajax callback

function validation() {
    var reply = testAjax();
    reply.done(function(data) {
        var x = document.getElementById("word").value;
        alert(data);
        ...
    })
}
Sign up to request clarification or add additional context in comments.

3 Comments

Ok. But we don't know which version of jQuery OP is using. It will work with recent ones. But from my experience old library version live long in big projects.
done, fail, always are alternative to deprecated success, error, complete methods.
It's perfectly clear to me. I only say that this code works in jQuery >= 1.5
1

Generic Function:

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);
  });
};

You may use this generic function to call anywhere in your application as:

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
});

5 Comments

We don't know which version of jQuery OP is using. It will work with recent ones. But from my experience old library version live long in big projects. The rest is just my personal opinion about this jQuery feature: I don't like this kind of spaghetti syntax and from OOP view this looks like done() returns object with fail() method. For me this is weird and harder to read than old dict based syntax. But I understand this is their (jQuery devs) design and from this point of view your code is OK.
Thanks for appreciating :)
@PrakashBhagat when i call my service url("test.movies.9pstudio.com:8080/wordchain/wordsearch")
In fact, You may use any url you want, in your case it's test.movies.9pstudio.com:8080/wordchain/wordsearch
it gives an error is Uncaught ReferenceError: textAjax is not defined
0

You should implement either success or complete handler as https://api.jquery.com/jQuery.ajax/ says.

Example:

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
    }
  });
}  

Comments

0

Use callback parameter in first function

function testAjax(callback) {
  $.ajax({
    url:...
    success: function(data) {
      callback(data); 
    }
  });
}

Comments

0

I think you are trying to use success callback function here as shown below:

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 = "";
}

15 Comments

i use a button to call function ,after click the button testAjax() is executed .....but still not working...
<input type="button" id="btn" name="btn" value="Button" onclick="testAjax()"/>
@SatyaPandu please check in console for any errors.
@SatyaPandu keep the console in chrome open and keep break points in both the functions and then do the click action again.
error is Uncaught ReferenceError: textAjax is not defined
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.