PDA

View Full Version : Ajax call to REST webservice


srini418
02-05-2010, 12:10 AM
Hi,
I am new to ajax , I need to call a REST based webservice using apache cxf. I am using Ajax to call the webservice with a JSON string.
The code that i am using is as follows
$.ajax({
async: false,
type: "POST",
url: iurl,
data: '{"abcdTO":{"term":36,"rate":"0","payment":500,"down":0,"miles":12000}}',
success: function(response)
{
alert(response);
},
error: function()
{
alert('Failure ');
}
});

The server side method for receiving the request is as follows
public AbcdOutputTO fetchModels(
AbcdInputTO abcdTO)
{
}

I am getting the input object as null, am i missing something or doing something wrong (which i am sure). please help me with this.

bdl
02-05-2010, 01:38 AM
You're certain the JSON object formatting you're sending is correct?

srini418
02-05-2010, 02:01 AM
The TO object is as such
AbcdInputTO has
term
rate
payment
down
miles

and i had used the following earlier but the same result
$.ajax({
async: false,
type: "POST",
url: iurl,
data: '{"term":36,"rate":"0","payment":500,"down":0,"miles":12000}',
success: function(response)
{
alert(response);
},
error: function()
{
alert('Failure ');
}
});

I am not sure if the Json object formatting is correct or not (i think its correct ... strings are in "" and numbers are as is), is there any configuration that needs to be done on the server side. I can post the WADL if needed.

bdl
02-05-2010, 03:13 AM
It's WADL? You're certain it accepts JSON, not XML? Post the data spec.

srini418
02-05-2010, 09:15 PM
application xmlns="http://research.sun.com/wadl/2006/10" xmlns:xs="http://www.w3.org/2001/XMLSchema">
- <grammars>
- <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="unqualified">
- <xs:complexType name="abcdOutputTO">
- <xs:complexContent>
- <xs:extension base="baseTO">
<xs:attribute name="aprType" type="xs:string" />
<xs:attribute name="monthlyPay" type="xs:double" use="required" />
<xs:attribute name="trimCd" type="xs:string" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
</grammars>
- <resources base="http://xxxx:80/xx/service/rest">
- <resource path="/">
- <resource path="/abcd">
<param name="abcdInputTO" style="template" />
- <method name="POST">
<request />
- <response>
<representation mediaType="application/json" />
</response>
</method>
</resource>
</resource>
</resources>
</application>

srini418
02-05-2010, 09:15 PM
i could not get what u ment by data spec