0

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

2 回答 2

1
  1. url: "ajaxsearch"很可能是url: "ajaxsearch.php"/url: "ajaxsearch.ashx"

  2. 您确定 dataType 是jsonp而不是json

  3. 不要eval()——这是邪恶的

  4. 检查为什么你没有在成功处理程序中结束的错误:

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

  5. 由于它是搜索,因此您应该使用type: 'GET'而不是 POST。POST 通常在将数据保存到服务器时使用(例如,在提交表单时。这只是一般准则。GET是默认值,因此您可以删除该属性。

于 2013-08-26T16:03:20.863 回答
0

我建议您首先尝试使 jQuery Ajax 使用简单的代码,然后进行修改。你可以试试下面的代码片段:

<!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>
于 2013-08-26T16:12:56.123 回答