我已經做了REST Web服務:調用使用JQuery Ajax調用休息Web服務,Web服務返回JSON字符串
package org.jboss.samples.rs.webservices;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("/MyRESTApplication")
public class HelloWorldResource {
@GET()
@Produces("application/json")
@Path("/dealInfo/{dealId}")
public String sayHello(@PathParam("dealId") int dealId) {
System.out.println("dealid......"+dealId);
switch(dealId) {
case 1 :System.out.println("employee id.....");
return "{'name':'George Koch', 'age':58}";
case 2:
return "{'name':'Peter Norton', 'age':50}";
default:
return "{'name':'unknown', 'age':-1}";
} // end of switch
}
}
當我去到Internet Explorer的地址欄中&類型的:
http://localhost:8080/nagarro-0.0.1-SNAPSHOT/MyRESTApplication/dealInfo/2
它給我:
{'name':'Peter Norton', 'age':50}
但是,當我把它用Ajax調用我一個JQuery方法。例如
$.ajax({
type: "GET",
url: "http://localhost:8080/nagarro-0.0.1-SNAPSHOT/MyRESTApplication/dealInfo/2",
data: "",
success: function(resp){
// we have the response
alert("Server said123:\n '" + resp + "'");
},
error: function(e){
alert('Error121212: ' + e);
}
});
我在這次調用中收到錯誤。
當我在IE中使用F12的調試,我得到以下以及
"Invalid JSON: {\'name\':\'Peter Norton\', \'age\':50}"
會有人告訴我,可能是我的電話的問題。
如何調用此Web服務? – eureka19 2016-10-06 09:01:38