Ajax 和 Java 网络服务奇怪错误
Ajax and Java web service strange error
我遇到了一个非常奇怪的错误。我成功 运行 我的代码并使用了网络服务。服务 return 200 代码。但是 jquery ajax 执行错误函数而不是成功。我 post 信息(控制台上没有):
Java脚本:
$(document).ready(
function () {
$.ajax({
url: 'Test/Service/hello',
dataType: 'json',
contentType: 'application/json',
data: {name: 'testing'},
error: function () {
$('#text').html('<p>An error has occurred</p>');
},
success: function (data) {
$("#text").html(data.d);
},
type: 'POST'
});
});
Java:
package com.morethansimplycode.webservice;
import javax.jws.WebParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/Service")
public class Service {
public Service() {
}
@POST
@Path("/hello")
@Consumes({MediaType.APPLICATION_JSON})
@Produces(MediaType.APPLICATION_JSON)
public String hello(@WebParam(name = "name") String txt) {
return "Hello " + txt + " !";
}
}
Chrome 信息:
Remote Address:[::1]:8080
Request URL:http://localhost:8080/WebService/Test/Service/hello
Request Method:POST
Status Code:200 OK
Response Headers
view source
Content-Length:17
Content-Type:application/json
Date:Tue, 01 Dec 2015 21:24:26 GMT
Server:GlassFish Server Open Source Edition 4.1
X-Powered-By:Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition 4.1 Java/Oracle Corporation/1.8)
Request Headers
view source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:es-ES,es;q=0.8,ca;q=0.6,en;q=0.4,pt;q=0.2,ru;q=0.2
Cache-Control:no-cache
Connection:keep-alive
Content-Length:9
Content-Type:application/json
Host:localhost:8080
Origin:http://localhost:8080
Pragma:no-cache
Referer:http://localhost:8080/WebService/
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payload
name=paco
预览:
Hello name=testing !
发生了什么事?
编辑 1:
适用于此调用:
$(document).ready(
function () {
$.ajax({
url: 'Test/Service/hello',
dataType: 'text',
contentType: 'application/json',
data: {name: 'paco'},
error: function () {
$('#etiqueta').html('An error has occurred');
},
success: function (data) {
console.debug(data);
$("#etiqueta").html('<p>' + data + '</p>');
},
type: 'POST'
});
}
);
但我不知道如何将 Web 服务中的对象作为 Json 发送到 JS,以便将其与 data.d 等一起使用...
编辑 2:
正确工作如下:
JS:
$(document).ready(
function () {
$.ajax({
url: 'Test/Service/hello',
dataType: 'json',
contentType: 'application/json',
data: {name: 'paco'},
error: function () {
$('#etiqueta').html('An error has occurred');
},
success: function (data) {
console.debug(data);
$("#etiqueta").html('<p>' + data.d + '</p>');
},
type: 'POST'
});
}
);
Java:
@Path("/Service")
public class Service {
public Service() {
}
/**
* This is a sample web service operation
*
* @param txt
* @return
*/
@POST
@Path("/hello")
@Consumes({MediaType.APPLICATION_JSON})
@Produces(MediaType.APPLICATION_JSON)
public String hello(@WebParam(name = "name") String txt) {
return "{\"d\": \"Hello " + txt + " !\"}";
}
}
而不是
public String hello(@WebParam(name = "name") String txt) {
return "Hello " + txt + " !";
}
尝试使用:
public String hello(@WebParam(name = "name") String txt) {
return "{\"d\" : \"Hello " + txt + " !\"}";
}
如果你return一些复杂的class,比如Product,它会被转换成json,但是如果你returnString,lib就不能转换它到 {key=value} 并认为您手动生成了 json。请参阅此 question 了解更多信息。
我遇到了一个非常奇怪的错误。我成功 运行 我的代码并使用了网络服务。服务 return 200 代码。但是 jquery ajax 执行错误函数而不是成功。我 post 信息(控制台上没有):
Java脚本:
$(document).ready(
function () {
$.ajax({
url: 'Test/Service/hello',
dataType: 'json',
contentType: 'application/json',
data: {name: 'testing'},
error: function () {
$('#text').html('<p>An error has occurred</p>');
},
success: function (data) {
$("#text").html(data.d);
},
type: 'POST'
});
});
Java:
package com.morethansimplycode.webservice;
import javax.jws.WebParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/Service")
public class Service {
public Service() {
}
@POST
@Path("/hello")
@Consumes({MediaType.APPLICATION_JSON})
@Produces(MediaType.APPLICATION_JSON)
public String hello(@WebParam(name = "name") String txt) {
return "Hello " + txt + " !";
}
}
Chrome 信息:
Remote Address:[::1]:8080
Request URL:http://localhost:8080/WebService/Test/Service/hello
Request Method:POST
Status Code:200 OK
Response Headers
view source
Content-Length:17
Content-Type:application/json
Date:Tue, 01 Dec 2015 21:24:26 GMT
Server:GlassFish Server Open Source Edition 4.1
X-Powered-By:Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition 4.1 Java/Oracle Corporation/1.8)
Request Headers
view source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:es-ES,es;q=0.8,ca;q=0.6,en;q=0.4,pt;q=0.2,ru;q=0.2
Cache-Control:no-cache
Connection:keep-alive
Content-Length:9
Content-Type:application/json
Host:localhost:8080
Origin:http://localhost:8080
Pragma:no-cache
Referer:http://localhost:8080/WebService/
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payload
name=paco
预览:
Hello name=testing !
发生了什么事?
编辑 1:
适用于此调用:
$(document).ready(
function () {
$.ajax({
url: 'Test/Service/hello',
dataType: 'text',
contentType: 'application/json',
data: {name: 'paco'},
error: function () {
$('#etiqueta').html('An error has occurred');
},
success: function (data) {
console.debug(data);
$("#etiqueta").html('<p>' + data + '</p>');
},
type: 'POST'
});
}
);
但我不知道如何将 Web 服务中的对象作为 Json 发送到 JS,以便将其与 data.d 等一起使用...
编辑 2:
正确工作如下:
JS:
$(document).ready(
function () {
$.ajax({
url: 'Test/Service/hello',
dataType: 'json',
contentType: 'application/json',
data: {name: 'paco'},
error: function () {
$('#etiqueta').html('An error has occurred');
},
success: function (data) {
console.debug(data);
$("#etiqueta").html('<p>' + data.d + '</p>');
},
type: 'POST'
});
}
);
Java:
@Path("/Service")
public class Service {
public Service() {
}
/**
* This is a sample web service operation
*
* @param txt
* @return
*/
@POST
@Path("/hello")
@Consumes({MediaType.APPLICATION_JSON})
@Produces(MediaType.APPLICATION_JSON)
public String hello(@WebParam(name = "name") String txt) {
return "{\"d\": \"Hello " + txt + " !\"}";
}
}
而不是
public String hello(@WebParam(name = "name") String txt) {
return "Hello " + txt + " !";
}
尝试使用:
public String hello(@WebParam(name = "name") String txt) {
return "{\"d\" : \"Hello " + txt + " !\"}";
}
如果你return一些复杂的class,比如Product,它会被转换成json,但是如果你returnString,lib就不能转换它到 {key=value} 并认为您手动生成了 json。请参阅此 question 了解更多信息。