简体   繁体   中英

Jquery Unable to make Ajax Request to Server

I am trying to make an AJAX Request through JQuery The below is my code .

But when i debugged through Mozilla Firebug i observed that ,there is no Request hitting to the Server .

Could anybody please tell me where i am doing wrong .

<%@page contentType="text/html" pageEncoding="UTF-8"%>



<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JQuery Example</title>
    </head>
    <body>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                         $.ajax({
                      url: 'ajax/balances',
                      processData: false,
                      timeout: 10000,
                      type: "POST",
                      contentType: "application/xml",
                      dataType: "json",
                      data: '<MyReq  user="' + User + '" katha="' + ivalitiKatha + '" />',
                     success: function(data) {

                     },
                     error : function() {
                             alert("Sorry, The requested property could not be found.");
                     }
             });

                });

        </script>
            </body>
</html>

This is my web.xml on server side

<servlet-mapping>
      <servlet-name>Jersey Web Application</servlet-name>
      <url-pattern>/ajax/*</url-pattern>
   </servlet-mapping>

也许在头部添加<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>身体有帮助!

First of all I would recommend moving the CDN JQuery into the head section of the website.

Secondly I have tested the code above and the issue looks to lie with the (data) you are posting in the JSON / AJAX request.

If you remove it or amend to JSON the request returns a result eg

$.ajax({
    url: 'test',
    processData: false,
    timeout: 10000,
    type: "POST",
    contentType: "application/json",
    dataType: "json",
    data: '{"foo": "bar"}',
    success: function(data) {
        alert('Success');                
    },
    error : function() {
        alert("Sorry, The requested property could not be found.");
    }
});​

You will need to format the data as a JSON request

data: '{"foo": "bar"}',

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM