java - jquery ajax call returns error on successful call to a servlet - Stack Overflow

I have a servlet that adds a user to a file on the server side.I invoke it with a jqueries ajax call.

I have a servlet that adds a user to a file on the server side.
I invoke it with a jqueries ajax call.
I can see on the server that the method is being called correctly and my user is added, but the error callback is being invoked on the jquery call. All the status text says is error.
Using firebug the response seems to be empty. Why can I not get a success jquery callback?

//Servlet Code
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String action = request.getParameter("action");
    String responseStr = "";
    if(action.equals("addUser")) 
    {  
      responseStr = addUser(request);       
    }
    System.out.println("Reponse:" + responseStr);  
    response.setContentType("application/json"); 
    response.setCharacterEncoding("utf-8"); 
    response.getWriter().println(responseStr);
}

private String addUser(HttpServletRequest request) throws IOException
{
    Storage s;

    s = Storage.load();
    String name = request.getParameter("name");
    String imageUrl = request.getParameter("imageUrl");
    User u = new User();
    u.setName(name);
    u.setImageUrl(imageUrl);
    s.addUser(u);
    s.save();
    return "success";
}

.

//javascript code
function addUser() {
    var name = $('#name').val();
    var imageUrl = $('#imageUrl').val();
    var url = "http://ws06525:8080/QCHounds/QCHoundServlet?action=addUser&name=${name}&imageUrl=${imageUrl}";
    url = url.replace("${name}", name);
    url = url.replace("${imageUrl}", imageUrl);
    $('#result').html(url);
    $.ajax({
      url: url,
      success: function( data ) {
         $('#result').html(data);
      },
      error: function(jqXHR, textStatus, errorThrown) 
      {
           alert("error: " + textStatus); 
           alert("error: " + errorThrown);
      }
    });


}

I have a servlet that adds a user to a file on the server side.
I invoke it with a jqueries ajax call.
I can see on the server that the method is being called correctly and my user is added, but the error callback is being invoked on the jquery call. All the status text says is error.
Using firebug the response seems to be empty. Why can I not get a success jquery callback?

//Servlet Code
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String action = request.getParameter("action");
    String responseStr = "";
    if(action.equals("addUser")) 
    {  
      responseStr = addUser(request);       
    }
    System.out.println("Reponse:" + responseStr);  
    response.setContentType("application/json"); 
    response.setCharacterEncoding("utf-8"); 
    response.getWriter().println(responseStr);
}

private String addUser(HttpServletRequest request) throws IOException
{
    Storage s;

    s = Storage.load();
    String name = request.getParameter("name");
    String imageUrl = request.getParameter("imageUrl");
    User u = new User();
    u.setName(name);
    u.setImageUrl(imageUrl);
    s.addUser(u);
    s.save();
    return "success";
}

.

//javascript code
function addUser() {
    var name = $('#name').val();
    var imageUrl = $('#imageUrl').val();
    var url = "http://ws06525:8080/QCHounds/QCHoundServlet?action=addUser&name=${name}&imageUrl=${imageUrl}";
    url = url.replace("${name}", name);
    url = url.replace("${imageUrl}", imageUrl);
    $('#result').html(url);
    $.ajax({
      url: url,
      success: function( data ) {
         $('#result').html(data);
      },
      error: function(jqXHR, textStatus, errorThrown) 
      {
           alert("error: " + textStatus); 
           alert("error: " + errorThrown);
      }
    });


}
Share Improve this question edited Sep 30, 2011 at 13:18 n4rzul asked Sep 30, 2011 at 12:49 n4rzuln4rzul 4,0788 gold badges47 silver badges66 bronze badges 2
  • How is the response successful if FireBug shows the response to be empty? – BNL Commented Sep 30, 2011 at 12:54
  • What I mean is that the server side functionality successfuly executes and adds my user, but the reponse is indeed empty, I can't understand why?? – n4rzul Commented Sep 30, 2011 at 13:06
Add a ment  | 

3 Answers 3

Reset to default 3

Aaargh! Feel like an idiot. It's a cross site scripting issue. I was testing the call to the server from the html file on disk so my browser address was

file://projects/myproject/content/Users.html <<< Fail

instead of:

http://myboxname:8080/appname/Users.html <<< Works

The actual code is fine...

use this for learn what is the problem, it will be better for get solution i think

error: function(e){
    alert(JSON.stringify(e))
}

For one thing the string "success" isn't valid json. If your ajax query is expecting json, that would fail it.

What if you returned "{ \"success\": true }" ?

EDIT

It looks like from your ajax call that the response shouldn't be json, why is your return content type json?

If it is true that firebug shows no response, your problem must be in the java code that writes the response.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742259416a4410626.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信