nirbhay mishra
nirbhay mishra

Reputation: 51

jquery ajax request success portion not working

I am having this ajax request but success portion is not working I have been working from past one hour but not able to get is there any thing I am missing.

$.ajax({
    url: "ajaxsearch",
    type: 'POST',
    dataType: "jsonp",
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    data: JSON.stringify(eval({
        name_startsWith: request.term
    })),

    success: function (data) {
        alert("yoo"); //What ever I write  do not get executed is any thing wrong
    }
});

Upvotes: 0

Views: 452

Answers (2)

Sandy
Sandy

Reputation: 1012

I will suggest you to first try to make the jQuery Ajax working with simple code then do modification. You can try the below code snippet:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript">
    $(function() {
        $("#btnSend").click(function(){
            var args = {
                    url : "http://localhost:8080/JqueryAjaxPrj/CustomerServlet",
                    type : "POST",
                    success : handleResponse,
                    dataType : "json",
                    data : {my-status: "test", my-name: "xyz"}
            };
            $.ajax(args);
        });
    });

    function handleResponse(response) {
        alert(response);
    }
</script>
</head>
<body>
    <button id="btnSend">Send Request</button>
    <br>
</body>

Upvotes: 0

Johan
Johan

Reputation: 35213

  1. url: "ajaxsearch" is most likely something like url: "ajaxsearch.php" / url: "ajaxsearch.ashx"

  2. Are you sure that the dataType is jsonp and not json?

  3. Don't eval() - it's evil

  4. Inspect the error on why you aren't ending up in the success handler:

    error: function(jqXhr, status, error) { /* handle error here */ }

  5. Since it's a search, you should use type: 'GET' instead of POST. POST is usually used when saving data to the server (like when submitting a form, for example. This is just a general guideline. GET is default, so you could just remove the property.

Upvotes: 1

Related Questions