Problem with Axis2 and JSON when calling web service in javascript
Hi all. I have a problem when calling Axis2 web service from javascript. I want to use it with JSON format. So as Axis2 has JSON support, I have set both messageFormatter and messageBuilder like it is in documentation. Also I am using DynamicResponseHandler.
Now, when I call it from javascript using GET method, it's working, but when I change it to POST, it not works. I am new to this, so I don't know, where is the problem. In Axis2 docs there is written that default is POST, so it should works, but for me not. Response never come back from server, and server is processing something till I stop him.
Here is my service class
package com.yms.tis.json;
import org.apache.axis2.context.OperationContext;
import org.apache.axis2.context.ServiceContext;
public class JSONService {
public void init(ServiceContext serviceContext) {
}
public void destroy(ServiceContext serviceContext) {
}
public void setOperationContext(OperationContext operationContext) {
}
public MenuItem[] getMenu() {
MenuItem[] menu = new MenuItem[3];
for (int i = 0; i < menu.length; i++) {
MenuItem menuItem = new MenuItem(i, "menuitem" + i,
"<div>toto je xxx content</div>");
menu[i] = menuItem;
}
return menu;
}
}
Here is the java script code:
this is working
<!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=UTF-8">
<title>Insert title here</title>
<script src="javascript/jquery-1.4.2.min.js" type="text/javascript"></script>
</head>
<body>
RESULT:<br />
<div id="result_panel">
</div>
</body>
<script type="text/javascript">
$.ajax({
url: 'services/JSONService/getMenu?response=application/json',
dataType: "json",
type : "GET",
success: function (xxx) {
$("#result_panel").html(xxx.getMenuResponse.retu rn[0].content); },
error: function (html) {
alert('ERROR');
}
});
</script>
</html>
and this not:
<!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=UTF-8">
<title>Insert title here</title>
<script src="javascript/jquery-1.4.2.min.js" type="text/javascript"></script>
</head>
<body>
RESULT:<br />
<div id="result_panel">
</div>
</body>
<script type="text/javascript">
$.ajax({
url: 'services/JSONService/getMenu',
dataType: "json",
contentType: "application/json; charset=utf-8",
type : "POST",
success: function(msg) {
$("#ReturnMessage").html(msg.d);
},
error: function (html) {
alert('ERROR');
}
});
</script>
</html>
Can you tell me what I've done wrong, please?
Thanks a lot for any help.