Unfortunately, I can confirm that Andrzej's beforeSend fix is the way to 
go on this one. Been doing it for a while now...

@Thierry: The dataType parameter in .ajax is just for jQuery to handle 
the response. It doesn't actually do anything for jQuery's request.

An additional jQuery quirk to think about when working with Restlet is 
that caching really messes things up with IE. Here's what I add every time:

$(function() {
     if($.browser.msie) {
         // Caching breaks Internet Explorer
         $.ajaxSetup({
             cache: false
         });
     }
}

Perhaps it's a good idea to set up a "Client Quirks" page on the Restlet 
wiki? Obviously not Restlet's fault that clients are broken, but we need 
them.

By the way, I had no issues ever with Ext-JS. The Ext-JS Core is 
liberally licensed, and is almost a drop-in replacement for jQuery on 
things like this.

-Tal

P.S. I really wish the browser JavaScript world would abandon the term 
"AJAX." It shows a lack of commitment to what these calls are really 
about -- REST.


On 12/16/2009 07:58 AM, Andrzej Berico wrote:
> Thierry,
>
> 1) Our JQuery Ajax call:
>
> $.ajax({
>                   type: 'GET',
>                   url: myUrl,
>                   success: onAjaxSuccess,
>                   error: onAjaxError,
>                  cache: false,
>                  beforeSend :
> function(xhr){xhr.setRequestHeader('Accept','application/xml')}
>               });
>
> Will generate Accept header of  "*/*, application/xml". We want
> "application/xml".
>
> 2) Our Restlet 1.1.5
>       
> @Override
> public Restlet createRoot() {
>               // Create a router Restlet.
>               Router router = new Router(getContext());
>       
>               // Attach our routers.
>               router.attach("/test", HelloWorldResource.class);
>               router.attach("/file/status", FileStatusResource.class);
>
>               return router;
>       }
>
>
> public FileStatusResource(Context context, Request request, Response
> response) {
>               super(context, request, response);
>               
>               // Read URI query strings.
>               Form form = request.getOriginalRef().getQueryAsForm();
>               if (form != null) {
>                       setUser(form.getFirstValue("user", isIgnoreCase()));
>                       setOriginalUri(form.getFirstValue("originalUri", 
> isIgnoreCase()));
>                       setAction(form.getFirstValue("action", isIgnoreCase()));
>               }
>               
>               // Variants that our resource generates.
>               getVariants().add(new Variant(MediaType.APPLICATION_JSON));
>               getVariants().add(new Variant(MediaType.APPLICATION_XML));
>               
>       }
>
> @Override
> public Representation represent(Variant variant) throws ResourceException {
>               Representation representation = null;
>               
>               // Validate request
>               if (StringUtils.isBlank(getUser())) {
>                       log.debug("GET FileStatusResource bad user. ");
>                       representation = representError(variant, new 
> ErrorMessage());
>                       
> getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
>                       return representation;
>               }
>               
>               representation = representGet(this,variant);
>               return representation;
>       }
>
> public final Representation representGet(CoralReefResource crr, Variant
> variant)
>                       throws ResourceException {
>               Representation representation = null;
>
>          log.debug("HTTP GET for : " + variant.getMediaType().getName());
> ...
>
>
>
>
> Thierry Boileau wrote:
>    
>> Hello Andrzej,
>>
>> I wonder if the source cause if your issue is out there.
>> Could you tell us how are built your resources? How do you send the
>> request using jquery?
>>
>> Best regards,
>> Thierry Boileau
>>
>>      
>>> Our Restlet resource server APPLICATION_JSON and APPLICATION_XML. A
>>> JQuery
>>> client, sets the Accept Type to "*/*, application/xml". So the Restlet
>>> negotiates that APPLICATION_JSON should be returned as expected. However,
>>> we
>>> want APPLICATION_XML to be served. The problem is that we are unable to
>>> remove the "*/*" as the first Accept type in JQuery. JQuery seems to tack
>>> on
>>> the "*/*" as an Accept Type to every request we send.
>>>
>>> Is there an elegant way in Restlet to filter the "*/*" values, e.g. ALL
>>> or
>>> APPLICATION_ALL? We can certainly get the Accept Headers in Restlet and
>>> re-arrange the Accept list ourselves to "override" the Restlet
>>> negotiation.
>>> We are thinking of filtering, after Restlet negotiation, based on the
>>> User-Agent header for this particular JQuery client, so as not to "break"
>>> the Restlet behaviour for other clients (which is what we expect.)
>>>
>>>        
>> ------------------------------------------------------
>> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2430776
>>
>>
>>      
>

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2430818

Reply via email to