On 29/05/12 13:23, Abhishek Sharma wrote:
For problem 2 I read json padding works only for GET requests. And CXF
provide interceptors for jsonp but what about POST?

Have a look at the CORS section:

http://cxf.apache.org/docs/jax-rs-cors.html

Regarding the problem1.
> *Please explain the significance of jaxrs:extension Mappings:*
>
> <jaxrs:extensionMappings>
>
> <entrykey=/"json"/ value=/"application/json"/ />
>
> <entrykey=/"xml"/ value=/"application/xml"/ />
>
> <entrykey=/"java"/ value=/"application/x-java-serialized-object"/ />
>
> </jaxrs:extensionMappings>

that allows you force the runtime to set HTTP Accept to a given value, ex, "GET /a.json' is equivalent to doing "GET /a" and explicitly setting Accept to application/json.

With POST you may want to try to use a _ctype query parameter, there are some default shortcuts available, including for 'json' & 'xml', but values like application/json can be explicitly provided. Perhaps you can also tweak jquery to issue the type you need, but I do not know how to do it

Cheers, Sergey


Thanks,

Abhishek

*From:*Abhishek Sharma
*Sent:* Tuesday, May 29, 2012 5:35 PM
*To:* [email protected]
*Subject:* How to hit jaxrs api using jquery ajax

Hi,

I’ve setup jaxrs services using cxf and able to run them using soap-ui
as well as thru the jaxrs:client. Now I need to hit these API using
jquery ajax.

I am facing two problems:

*Prob1:*

Not able to override the Content-Type to application/json or even
application/xml and either it remain to
application/x-www-form-urlencoded or null.

*JS CODE:*

*function*ajaxCall(url,data){

$.ajax({

url: url,

type: 'POST',

crossDomain: *true*,

async: *true*,

dataType: 'application/xml',

data: data,

contentType: 'application/xml; charset=UTF-8', >> if I remove this line
then content type is sent as:application/x-www-form-urlencoded

/*beforeSend: function(_xhr_) {

xhr.setRequestHeader( "Content-Type", "application/_xml_" );

},*/

success: *function*(data, textStatus, jqXHR) {

$("#res").html("<br>Success:<br>"+data+"<br>"+textStatus+"<br>"+jqXHR);

},

error: *function*(jqXHR, textStatus, errorThrown){

$("#res").html("<br>Error:<br>"+jqXHR+"<br>"+textStatus+"<br>"+errorThrown);

}

});

}

*CXF Service Interface:*

@Path("/hotels/v1.0")

@Consumes({"application/xml", "application/json"})

@Produces({"application/xml", "application/json"})

*public**interface*ISuggestService {

@POST

@Path("/suggest")

WSHotelSuggestResponse suggest(WSHotelSuggestRequest suggestRequest);

*Please explain the significance of jaxrs:extension Mappings:*

<jaxrs:extensionMappings>

<entrykey=/"json"/ value=/"application/json"/ />

<entrykey=/"xml"/ value=/"application/xml"/ />

<entrykey=/"java"/ value=/"application/x-java-serialized-object"/ />

</jaxrs:extensionMappings>

*Problem 2:*

How to hit the POST API cross domain ? (The above problem comes even on
same domain).

Regards,

*Abhishek Sharma**|* -

| MakeMyTrip.com, 243 SP Infocity, Udyog Vihar Phase 1, Gurgaon, Haryana
- 122 016, India

http://www.mailmktg.makemytrip.com/signature/images/bulb.gif*What's
new?: Inspire *- Discover an inspiring new way to plan and book travel
online <http://inspire.makemytrip.com/inspire/>.

http://www.mailmktg.makemytrip.com/signature/images/MMT-signature-footer-V4.gif
<http://www.makemytrip.com/>

http://www.mailmktg.makemytrip.com/signature/images/map-icon.gif
<http://www.makemytrip.com/support/gurgaon-travel-agent-office.php>
*Office Map*

        

http://www.mailmktg.makemytrip.com/signature/images/facebook-icon.gif
<http://www.facebook.com/pages/MakeMyTrip-Deals/120740541030?ref=search&sid=100000077980239.1422657277..1>
*Facebook*

        

http://www.mailmktg.makemytrip.com/signature/images/twitter-icon.gif
<http://twitter.com/makemytripdeals>
*Twitter*



--
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Reply via email to