【发布时间】:2015-01-20 14:17:54
【问题描述】:
我必须使用将由 RestFulWebServices 使用的 JQuery+Ajax 发送 JSON 对象数据。在后端,我使用的是 hibernate(ver 4)+Maven(ver 3)+spring(ver 4)、MySql 数据库和 ApacheTopcat 服务器(ver 7)。但是我的 JqueryAjax 代码 index.html 客户端没有通过服务器发送数据。请帮我搜索但是他们在我的 Jquery Ajax 部分没有错误。如果您需要其他任何东西,请告诉我,我会从这里过去。
form.html
<!DOCTYPE html>
<html lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
alert("1");
$("button").click(function(){
alert("2");
var custName = $('#custName').val();
var custMobile = $('#custMobile').value;
var custEmail = $('#custEmail').value;
var custAddress = $('#custAddress').value;
var JSONObject={"custName":custName, "custMobile": custMobile, "custEmail":custEmail,"custAddress":custAddress};
/*
var jsonData=JSON.stringify({
"custName": "Navin1",
"custMobile": "876532468",
"custEmail": "abc@gmal.com",
"custAddress": "BAnaore"
});
*/
var jsonData = JSON.stringify( JSONObject );
$.ajax({
url: "http://localhost:8080/HomeServiceProvider/customer/saveCustomer",
type: "POST",
dataType: "json",
data: jsonData,
contentType: "application/json; charset=utf-8",
async: false,
cache: false,
processData:false,
success: function(response){
alert("scucess");
alert(JSON.stringify(response));
},
error: function(err){
alert("Fail");
alert(JSON.stringify(err));
}
});
});
});
</script>
</head>
<body>
<form>
<fieldset style="text-align:right; width:300px">
<legend><b>Registration Form</b></legend>
Name <input type="text" id="custName" name="custName"/><br/>
Mobile No <input type="text" id="custMobile" name="custMobile"/><br/>
Email <input type="text" id="custEmail" name="custEmail"/><br/>
Address <input type="text" id="custAddress" name="custAddress"/><br/>
<button>Save Data</button>
</fieldset>
</form>
</body>
</html>
我的服务器网址是 http://localhost:8080/HomeServiceProvider/customer/saveCustomer
我的客户休息控制器是 CustomerRestController
@RestController
@RequestMapping("/customer")
public class CustomerRestController {
private static Logger log = LogManager.getLogger(CustomerRestController.class);
@Value("${msg.customeradded}")
private String message;
@Value("${msg.successcode}")
private int code;
@Autowired
private CustomerService customerService;
@RequestMapping(value = "/saveCustomer", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Status saveCustomer(@RequestBody Customer customer){
try {
customerService.saveCustomer(customer);
return new Status(code, message);
} catch (Exception e) {
return new Status(0, e.toString());
}
}
@RequestMapping(value="/getAllCustomer",method=RequestMethod.GET, headers="Accept=application/json")
public @ResponseBody List<Customer> getAllCustomer(){
List<Customer> customers = null;
try {
customers = customerService.getAllCustomer();
log.info("Size:"+customers.size());
log.info("customers:"+customers);
} catch(Exception e) {
e.printStackTrace();
}
return customers;
}
}
MyCustomer 类 Customer.Java 用于通过 Hibernate 制作表格
@Entity
@Table(name = "customer", catalog = "service4homes")
public class Customer implements java.io.Serializable {
private Integer CId;
private String custName;
private String custMobile;
private String custEmail;
private String custAddress;
public Customer() {
}
public Customer(String custName, String custMobile, String custAddress) {
this.custName = custName;
this.custMobile = custMobile;
this.custAddress = custAddress;
}
public Customer(String custName, String custMobile, String custEmail,
String custAddress) {
this.custName = custName;
this.custMobile = custMobile;
this.custEmail = custEmail;
this.custAddress = custAddress;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "c_id", unique = true, nullable = false)
public Integer getCId() {
return this.CId;
}
public void setCId(Integer CId) {
this.CId = CId;
}
@Column(name = "cust_name", nullable = false, length = 50)
public String getCustName() {
return this.custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
@Column(name = "cust_mobile", nullable = false, length = 13)
public String getCustMobile() {
return this.custMobile;
}
public void setCustMobile(String custMobile) {
this.custMobile = custMobile;
}
@Column(name = "cust_email", length = 100)
public String getCustEmail() {
return this.custEmail;
}
public void setCustEmail(String custEmail) {
this.custEmail = custEmail;
}
@Column(name = "cust_address", nullable = false, length = 300)
public String getCustAddress() {
return this.custAddress;
}
public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
}
}
当我运行代码时我得到了什么 form.index 数据
点击按钮后
【问题讨论】:
-
你确定它没有发送到服务器吗?您是否在请求/响应周期中检查过浏览器的控制台?
-
@Jay Blanchard,感谢您的回复我没有发送数据,
-
你的点击功能失败了?
-
@Jay Blanchard,我想告诉你,我项目的架构分为intp UI+RestFull Webservices+Database,UI部分可以在任何地方,但不会在Project中。我可以获取数据但无法发送数据,请检查获取数据的代码ideone.com/f7JJAG
-
@Jay Blanchard,不打印警报(“2”),所以它不会失败..
标签: java jquery mysql json hibernate