java - Show error in jquery ajax call for jax-rs(resteasy) -
i have 2 class 1 userinformation , other useraddress, have added useradress class in userinformation class created setter , getter that. , create web service using jax-rs(resteasy) when send request using ajax call shows erroe. code following.
this useraddress class:
package com.my.dto; import java.io.serializable; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlrootelement; import org.codehaus.jackson.annotate.jsonproperty; import org.codehaus.jackson.map.annotate.jacksoninject; @xmlrootelement(name="useraddress") public class useraddress implements serializable{ private int streetno; private string city; private string state; private int pincode; @xmlelement public int getstreetno() { return streetno; } public void setstreetno(int streetno) { this.streetno = streetno; } @xmlelement public string getcity() { return city; } public void setcity(string city) { this.city = city; } @xmlelement public string getstate() { return state; } public void setstate(string state) { this.state = state; } @xmlelement public int getpincode() { return pincode; } public void setpincode(int pincode) { this.pincode = pincode; } } this userinformation class: in class have create setter , getter useraddress class.
package com.my.dto; import java.io.serializable; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlrootelement; import org.apache.http.entity.serializableentity; import org.codehaus.jackson.annotate.jsonproperty; @xmlrootelement(name="userinformation") public class userinformation implements serializable { private string name; private int age; private int roll; private useraddress useradd; @xmlelement public useraddress getuseradd() { return useradd; } public void setuseradd(useraddress useradd) { this.useradd = useradd; } @xmlelement public string getname() { return name; } public void setname(string name) { this.name = name; } @xmlelement public int getage() { return age; } public void setage(int age) { this.age = age; } @xmlelement public int getroll() { return roll; } public void setroll(int roll) { this.roll = roll; } } this resteasy web service both , post method :
package com.my.app; import javax.jws.soap.soapbinding.use; import javax.ws.rs.consumes; import javax.ws.rs.get; import javax.ws.rs.post; import javax.ws.rs.path; import javax.ws.rs.produces; import javax.ws.rs.core.mediatype; import javax.ws.rs.core.response; import org.codehaus.jettison.json.jsonobject; import com.my.dto.useraddress; import com.my.dto.userinformation; import com.my.dto.usersuccess; @path("/info") public class showuserinfo { @get @path("/get") @produces(mediatype.application_json) public userinformation getproductinjson() { userinformation infoobj = new userinformation(); useraddress useraddress=new useraddress(); useraddress.setcity("new delhi"); useraddress.setstate("delhi"); useraddress.setstreetno(447); useraddress.setpincode(110092); infoobj.setname("ravi raj"); infoobj.setage(21); infoobj.setroll(101); infoobj.setuseradd(useraddress); return infoobj; } @post @path("/post") @consumes(mediatype.application_json) @produces(mediatype.application_json) public response createproductinjson(userinformation infoobj) { return response.status(200).entity(infoobj).build(); } } following jquery ajax call , post request web service.
this call get:
var uri="http://localhost:8080/webserviceusingrestful/"; var studentdata; function checkwebservice(){ $.ajax({ url : uri+"rest/info/get" , type : 'get', contenttype : 'application/json; charset=utf-8', // request data type datatype : 'json', cache: false, success : function(data, status, req) { alert("pass calling api"); var responsejsonobj = json.parse(req.responsetext); printdata(responsejsonobj); }, error : function(req, status, error) { alert("fail calling api"); }, // set user basic authentication data request header /*beforesend : setheader*/ }); } this working fine.i have got json array. right.
but when post data service using code shows error.
function insertdata(){ var request = new object(); request.name=$('#usernamefld').val(); request.roll=$('#rollfld').val(); request.age=$('#agefld').val(); request.useradd.streetno=$('#streetfld').val(); request.useradd.city=$('#cityfld').val(); request.useradd.state=$('#statefld').val(); request.useradd.pincode=$('#pinfld').val(); $.ajax({ url : uri+"rest/info/post" , type : 'post', contenttype : 'application/json; charset=utf-8', data : json.stringify(request), // request data type datatype : 'json', cache: false, success : function(data, status, req) { alert("api working fine..!!"); var responsejsonobj = json.parse(req.responsetext); $('#insertdivid').show(); }, error : function(req, status, error) { alert("fail calling api"); }, // set user basic authentication data request header /*beforesend : setheader*/ }); } this call show error streetno this: uncaught typeerror: cannot set property 'streetno' of undefined if placed code .
function insertdata(){ var request = new object(); request.name=$('#usernamefld').val(); request.roll=$('#rollfld').val(); request.age=$('#agefld').val(); request.useradd{"city":$('#cityfld').val(),"streetno":$('#streetfld').val(),"state":$('#statefld').val(),"pincode":$('#pinfld').val()}; $.ajax({ url : uri+"rest/info/post" , type : 'post', contenttype : 'application/json; charset=utf-8', data : json.stringify(request), // request data type datatype : 'json', cache: false, success : function(data, status, req) { alert("api working fine..!!"); var responsejsonobj = json.parse(req.responsetext); $('#insertdivid').show(); }, error : function(req, status, error) { alert("fail calling api"); }, // set user basic authentication data request header }); } this working fine not valid way send request object using ajex call.
please go through code , give me idea problem. alot..!! note: if 1 have jax-rs tutorial link ..please add link me. thanks
you need declare request.useradd, before adding it
function insertdata(){ var request = new object(); request.name=$('#usernamefld').val(); request.roll=$('#rollfld').val(); request.age=$('#agefld').val(); request.useradd = {} // <--- declare useradd request.useradd.streetno=$('#streetfld').val(); request.useradd.city=$('#cityfld').val(); request.useradd.state=$('#statefld').val(); request.useradd.pincode=$('#pinfld').val(); but why is
data : json.stringify(request) not valid request?
regards
Comments
Post a Comment