我在java中实现了一个RESTful Web服务,它将数据插入到MySQL数据库中,我在mozila firefox和google chrome中使用了POSTER测试了这个。我的Web服务接受带有POST请求的String,现在我无法使用JS使用WEB SERVICE:在WEB SERVICE URL上发出POST请求的代码如下:
$.ajax({
url: 'http://localhost:8080/AgentWS/webresources/Items',
type: 'POST',
contentType: 'application/xml',
dataType: 'xml',
data: 'content='+content,
success: function (data) {
alert(content);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Error: " + errorThrown);
}
});
成功警报功能也不会显示加上对话框错误显示一个对话框:错误:上面'
服务器端代码是:
@POST
@Consumes("application/xml")
@Produces("application/xml")
public String postXml(String content) {
//TODO
// return Response.created(context.getAbsolutePath()).build();
StringTokenizer sp = new StringTokenizer(content, "&");
String agentName = sp.nextToken();
String agentId = sp.nextToken();
String agentState = sp.nextToken();
String agentExtension = sp.nextToken();
String agentDeviceState = sp.nextToken();
String agentDeviceStateChangeTime = sp.nextToken();
DBConection conn = new DBConection();
conn.insertAgentActivityInfo(agentName, agentId, agentState, agentExtension, agentDeviceState, agentDeviceStateChangeTime);
return agentName + " " + agentId + " " + agentState + " " + agentExtension + " " + agentDeviceState + " " + agentDeviceStateChangeTime;
}
答案 0 :(得分:0)
我的猜测是data: 'content='+content,
向服务器发布了无效的XML,并且您收到 500内部服务器错误。您可以尝试将data
设置为仅仅XML内容吗?像
...
data: content,
....
如果你想要的只是一个简单的帖子
@POST
public String postXml(String content) {
//TODO
// return Response.created(context.getAbsolutePath()).build();
StringTokenizer sp = new StringTokenizer(content, "&");
String agentName = sp.nextToken();
...
return agentName + " " + agentId + " " + agentState + " " + agentExtension + " " + agentDeviceState + " " + agentDeviceStateChangeTime;
}
$.ajax({
url: '/AgentWS/webresources/Items',
type: 'POST',
data: 'content=1&2&3&4&5&5',
success: function (data) {
alert(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Error: " + errorThrown);
}
});
答案 1 :(得分:0)
我认为问题在于你发送的数据,
data: 'content='+content
应替换为
data: {content:'content='+content}
并检查您在服务器端正在做什么
答案 2 :(得分:0)
这就是我的想法,您应该在服务器端编写一个模型类Agent,如下所示:
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Agent
{
private String agentName;
private String agentId;
private String agentState;
private String agentExtension;
private String agentDeviceState;
private String agentDeviceStateChangeTime;
public String getAgentName() {
return agentName;
}
public void setAgentName(String agentName) {
this.agentName = agentName;
}
public String getAgentId() {
return agentId;
}
public void setAgentId(String agentId) {
this.agentId = agentId;
}
public String getAgentState() {
return agentState;
}
public void setAgentState(String agentState) {
this.agentState = agentState;
}
public String getAgentExtension() {
return agentExtension;
}
public void setAgentExtension(String agentExtension) {
this.agentExtension = agentExtension;
}
public String getAgentDeviceState() {
return agentDeviceState;
}
public void setAgentDeviceState(String agentDeviceState) {
this.agentDeviceState = agentDeviceState;
}
public String getAgentDeviceStateChangeTime() {
return agentDeviceStateChangeTime;
}
public void setAgentDeviceStateChangeTime(String agentDeviceStateChangeTime) {
this.agentDeviceStateChangeTime = agentDeviceStateChangeTime;
}
}
你应该稍微改变你所拥有的服务器休息服务:
@POST
@Consumes("application/xml")
@Produces("application/xml")
public String postXml(Agent agent) {}
在此方法中,您可以使用传递的“agent”对象来检索客户端发送的所有值,如agent.getAgentName()
现在应该包含以下内容的有效负载(或请求正文):
<Agent>
<agentName></agentName>
<agentId></agentId>
<agentState></agentState>
<agentExtension></agentExtension>
<agentDeviceState></agentDeviceState>
<agentDeviceStateChangeTime></agentDeviceStateChangeTime>
</Agent>
我希望它有所帮助。
答案 3 :(得分:0)
如果没有错误代码和消息,很难猜到,但是:
更改
data: 'content='+content
到
data: { content : content } // format as json
在你的java资源中:
@POST
@Consumes(MediaType.APPLICATION_JSON)
public String postXml(String content)
// read content as json