Note that there are some explanatory texts on larger screens.

plurals
  1. POShow error in jquery ajax call for jax-rs(resteasy)
    primarykey
    data
    text
    <p>I have two class one UserInformation and other is UserAddress, i have added UserAdress class in UserInformation class created setter and getter for that. And create a web service using JAX-RS(RESTEasy) but when i send request using ajax call it shows erroe. My code are following.</p> <p>This is My UserAddress Class:</p> <pre><code>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; } } </code></pre> <p>This is my UserInformation Class: In this class i have create setter and getter for UserAddress class.</p> <pre><code>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; } } </code></pre> <p>This is my RESTEasy web service for both get and post method :</p> <pre><code>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(); } } </code></pre> <p>Following are my jquery ajax call for get and post request for web service.</p> <p>This is my call for get:</p> <pre><code>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 into request header /*beforeSend : setHeader*/ }); } </code></pre> <p>This is working fine.i have got json array. Which is right.</p> <p>But when we post data for service using this code this shows error.</p> <pre><code>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 into request header /*beforeSend : setHeader*/ }); } </code></pre> <p>This call show error for streetNo like this: Uncaught TypeError: Cannot set property 'streetNo' of undefined but if we placed code like this .</p> <pre><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 into request header }); } </code></pre> <p>This is working fine but this is not a valid way send request object using ajex call.</p> <p>Please go through my code and give me any idea for this problem. Thanks alot..!! Note: If any one have good JAX-RS tutorial link ..please add link for me. Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload