otis92
otis92

Reputation: 37

What causes the error to fire in an Ajax request?

So, I need to make an Ajax request for each stock symbol entered into a text box. In my ajax request, I attempt to send a string holding one symbol to my servlet. Unfortunately, when I attempt to make my request, the error message fires. What am I doing wrong here?

$(document).ready(function(){

    $('#submit').click(function(event){

            var singleStock = $('#stocks').val();
            var stocklist = singleStock.split(" ");
            var i;
            for(i=0; i<stocklist.length; i++){
                doAjaxRequest(stocklist[i]);
            }


         });
    });

function doAjaxRequest(data) {
    // replace the ??? with appropriate values      
            $.ajax({
                type : "POST", 
                url : "./QuoteServlet", 
                data : {"stock":data}, 
                dataType : "json",
                success : drawTable, 
                error : alert("Error")
            });
        }

Upvotes: 0

Views: 3694

Answers (2)

Nwachukwu A. Nnaemeka
Nwachukwu A. Nnaemeka

Reputation: 369

The error param needs to be a function, try this

    $.ajax({
            type : "POST", 
            url : "./QuoteServlet", 
            data : {"stock":data}, 
            dataType : "json",
            success : drawTable,
      error: function() {
         alert ("error");
          }
          });

Upvotes: 0

Barmar
Barmar

Reputation: 782166

The error: parameter must be a function. You're calling alert() unconditionally before you send the AJAX request, and setting the error parameter to what it returns.

function doAjaxRequest(data) {
    // replace the ??? with appropriate values      
            $.ajax({
                type : "POST", 
                url : "./QuoteServlet", 
                data : {"stock":data}, 
                dataType : "json",
                success : drawTable, 
                error : function(jqXHR, textStatus, errorThrown) { alert("Error: Status: "+textStatus+" Message: "+errorThrown); }
            });
        }

Upvotes: 1

Related Questions