简体   繁体   English

通过jQuery调用Web服务并将对象传递给接受java对象的rest Web服务

[英]Calling web service through jQuery and passing object to rest web service which accepts java object

My web service is working fine when I am testing it through rest client but when I try to call it through jQuery it fails .当我通过 rest 客户端对其进行测试时,我的 Web 服务工作正常,但是当我尝试通过 jQuery 调用它时却失败了。 the main problem is basically with serialization of JSON data into the object.主要问题基本上是将 JSON 数据序列化到对象中。

My web service is like this我的网络服务是这样的

$.ajax({
                type: "POST",
                data: JSON.stringify({"userId":124,"emailId":"ranjeet@triconinfotec.com","role":"instrutor","date":"2014-08-01","target":"Section",
                    "sectionId":234,"sectionName":"Economics","assignmentId":9991,"assignmentName":"EZT","isbn":"124XSD234","courseId":33,
                    "courseName":"GeneralEconomics","ipaddress":"192.168.1.210","pageId":"sd345"}),                                 

                url: "http://localhost:7001/connect/restservices/insight/assignmentgraph/connecttrack/activity",
                contentType: 'application/json',
                dataType: 'json',
                success: function(){  
                    // we have the response  
                    alert("Success");  
                  },  
                  error: function(e){  
                    alert('Error: ' + e);  
                  }  

This web service get called but its goes to error and display error [object object].此 Web 服务被调用,但会出错并显示错误 [对象对象]。

I think the problem is with serialization date in the content because in my Java file this class date has a date type instead of string.我认为问题在于内容中的序列化日期,因为在我的 Java 文件中,此类日期具有日期类型而不是字符串。

Change the error handler as below, and then investigate the response:如下更改错误处理程序,然后调查响应:

error: function(x, e) {  
   alert('Status code: ' + x.status + ', Error: ' + e);  
}

Your web service needs to know how to convert a String to a Date.您的 Web 服务需要知道如何将字符串转换为日期。 You didn't mention what framework you're using on the server to create your web service, but I imagine it has the ability to setup a converter.您没有提到您在服务器上使用什么框架来创建您的 Web 服务,但我想它具有设置转换器的能力。 If not, a quick and dirty work around would be to change the type of your field to String, and then once the request from the client comes in, use something like SimpleDateFormat to parse the String into a Date and store it in another field.如果没有,一个快速而肮脏的解决方法是将字段的类型更改为字符串,然后一旦来自客户端的请求进来,使用类似 SimpleDateFormat 的东西将字符串解析为日期并将其存储在另一个字段中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM