Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Ajax.Request fails, while vanilla XHR works fine
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Walter Lee Davis  
View profile  
 More options Apr 10 2012, 10:50 pm
From: Walter Lee Davis <wa...@wdstudio.com>
Date: Tue, 10 Apr 2012 22:50:10 -0400
Local: Tues, Apr 10 2012 10:50 pm
Subject: Ajax.Request fails, while vanilla XHR works fine
Same browser (Safari.latest) on the same computer, the Prototype method gives me a security failure (Origin [my host] is not allowed by Access-Control-Allow-Origin.) while the long-hand XHR (inside a Prototype observer) just works without any comment:

Prototype:
  $('zip').observe('change',function(evt){
    var elm = this;
    new Ajax.Request('http://zip.elevenbasetwo.com', {
      method: 'get',
      parameters: {zip: $F(elm)},
      onComplete: function(transport){
        var data = responseText.evalJSON();
        $('city').setValue(data.city);
        $('state').setValue(data.state);
      }
    });
 });

XHR:
  $('zip').observe('change',function(evt){
    var client = new XMLHttpRequest();
    client.open("GET", "http://zip.elevenbasetwo.com?zip=" + $F(this), true);
    client.onreadystatechange = function() {
      if(client.readyState == 4) {
        var data = client.responseText.evalJSON();
        $('city').setValue(data.city);
        $('state').setValue(data.state);
      };
    };
    client.send();
  });

I don't trust this to work in the range of browsers supported by Prototype, naturally, so I'd really like to know what I could do to get it to work in Prototype.

Thanks,

Walter


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Walter Lee Davis  
View profile  
 More options Apr 10 2012, 10:52 pm
From: Walter Lee Davis <wa...@wdstudio.com>
Date: Tue, 10 Apr 2012 22:52:48 -0400
Local: Tues, Apr 10 2012 10:52 pm
Subject: Re: [Proto-Scripty] Ajax.Request fails, while vanilla XHR works fine
Typo, fixed; still fails, same error, before ever reaching this line.

On Apr 10, 2012, at 10:50 PM, Walter Lee Davis wrote:

>        var data = responseText.evalJSON();

s/b var data = transport.responseText.evalJSON();

Walter


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jason  
View profile  
 More options Apr 11 2012, 9:00 pm
From: Jason <jwestbr...@gmail.com>
Date: Wed, 11 Apr 2012 18:00:04 -0700 (PDT)
Local: Wed, Apr 11 2012 9:00 pm
Subject: Re: Ajax.Request fails, while vanilla XHR works fine

Have you tried to just use

 new Ajax.Request('index.php', .........);

vs the full URL

from the Ajax.Request source code

* - url (String): The URL to fetch. When the _same-origin_ policy is
in
* effect (as it is in most cases), `url` **must** be a relative URL or
an
* absolute URL that starts with a slash (i.e., it must not begin with
* `http`).

On Apr 10, 7:52 pm, Walter Lee Davis <wa...@wdstudio.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Walter Lee Davis  
View profile  
 More options Apr 11 2012, 11:01 pm
From: Walter Lee Davis <wa...@wdstudio.com>
Date: Wed, 11 Apr 2012 23:01:19 -0400
Local: Wed, Apr 11 2012 11:01 pm
Subject: Re: [Proto-Scripty] Re: Ajax.Request fails, while vanilla XHR works fine
I understand the SOP, I was wondering why it worked when I violated SOP from a hard-coded XHR request. Under the hood, it is my understanding that Prototype sets up a very similar XHR request anyway. It doesn't make any sense that the one would work while the other did not. This server is specifically kinked to allow a request such as this, or else the SOP would cause it to fail no matter what. Can anyone point out the flaw in my logic here?

Walter

On Apr 11, 2012, at 9:00 PM, Jason wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Victor  
View profile  
 More options Apr 12 2012, 12:14 pm
From: Victor <vkhomyac...@gmail.com>
Date: Thu, 12 Apr 2012 09:14:51 -0700 (PDT)
Local: Thurs, Apr 12 2012 12:14 pm
Subject: Re: Ajax.Request fails, while vanilla XHR works fine

> Same browser (Safari.latest) on the same computer, the Prototype method
> gives me a security failure (Origin [my host] is not allowed by
> Access-Control-Allow-Origin.) while the long-hand XHR (inside a Prototype
> observer) just works without any comment:

> Two differences I can notice:

1. Prototype sets request headers 'X-Requested-With',
'X-Prototype-Version', 'Accept' with setRequestHeaders() - you don't
2. Prototype calls send with null argument
    this.transport.send(null); // Prototype
vs
    client.send(); // your raw XHR

If you can test your code with these two changes - will it raise error?

  $('zip').observe('change', function(evt) {
    var client = new XMLHttpRequest();
    client.open("GET", "http://zip.elevenbasetwo.com?zip=<http://zip.elevenbasetwo.com/?zip=>"
+ $F(this), true);
    client.onreadystatechange = function() {
      if(client.readyState == 4) {
        var data = client.responseText.evalJSON();
        $('city').setValue(data.city);
        $('state').setValue(data.state);
      };
    };
*    client.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
    client.setRequestHeader('X-Prototype-Version', '1.7');
*
*    client.setRequestHeader('Accept', 'text/javascript, text/html,
application/xml, text/xml, */*');*
    client.send(*null*);
  });


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Victor  
View profile  
 More options Apr 12 2012, 12:20 pm
From: Victor <vkhomyac...@gmail.com>
Date: Thu, 12 Apr 2012 09:20:29 -0700 (PDT)
Local: Thurs, Apr 12 2012 12:20 pm
Subject: Re: Ajax.Request fails, while vanilla XHR works fine

Is header 'Access-Control-Allow-Origin' present in responses for both
examples?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Walter Lee Davis  
View profile  
 More options Apr 12 2012, 3:57 pm
From: Walter Lee Davis <wa...@wdstudio.com>
Date: Thu, 12 Apr 2012 15:57:01 -0400
Local: Thurs, Apr 12 2012 3:57 pm
Subject: Re: [Proto-Scripty] Re: Ajax.Request fails, while vanilla XHR works fine

On Apr 12, 2012, at 12:14 PM, Victor wrote:

Thanks very much for the suggestion. It looks as though ANY setRequestHeader invocation at all is enough to scuttle the request. I tried commenting one, then two, then all of them out. Without those three lines, the request works fine -- even with the null in the send. But add any one of them back, and the request fails. It seems to be an issue on their server (BaseHTTP/0.3 Python/2.7.1+ according to FireBug). I'll file a bug and see what happens.

Walter


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Victor  
View profile  
 More options Apr 13 2012, 5:26 am
From: Victor <vkhomyac...@gmail.com>
Date: Fri, 13 Apr 2012 02:26:06 -0700 (PDT)
Local: Fri, Apr 13 2012 5:26 am
Subject: Re: [Proto-Scripty] Re: Ajax.Request fails, while vanilla XHR works fine

> It seems to be an issue on their server (BaseHTTP/0.3 Python/2.7.1+
> according to FireBug). I'll file a bug and see what happens.

XHR without setRequestHeader sends usual 'GET' request:

Request URL: http://zip.elevenbasetwo.com/?zip=a
Request Method: GET
Status Code: 404 Not Found
*Request Headers*
Accept: */*
Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.3
Accept-Encoding: gzip,deflate,sdch
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
Connection: keep-alive
Host: zip.elevenbasetwo.com
Origin: https://groups.google.com
User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.19 (KHTML, like
Gecko) Chrome/18.0.1025.142 Safari/535.19
*Response Headers*
Access-Control-Allow-Origin: *
Connection: close
Content-Type: text/plain
Date: Fri, 13 Apr 2012 09:16:07 GMT
Server: BaseHTTP/0.3 Python/2.7.1+

XHR with any setRequestHeader sends first 'OPTIONS' request (so-called
preflight<https://developer.mozilla.org/En/HTTP_Access_Control#Preflighted_requ...>
):

Request URL: http://zip.elevenbasetwo.com/?zip=a
Request Method: OPTIONS
Status Code: 501 Not Implemented
*Request Headers*
Accept: */*
Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.3
Accept-Encoding: gzip,deflate,sdch
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
Access-Control-Request-Headers: x-xxx, origin
Access-Control-Request-Method: GET
Connection: keep-alive
Host: zip.elevenbasetwo.com
Origin: https://groups.google.com
User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.19 (KHTML, like
Gecko) Chrome/18.0.1025.142 Safari/535.19
*Response Headers*
Connection: close
Content-Type: text/html
Date: Fri, 13 Apr 2012 09:20:32 GMT
Server: BaseHTTP/0.3 Python/2.7.1+

It seems that some browsers (like Firefox and WebKit-based) may send CORS
requests without preflight for text/plain resources when no additional
headers are set. In other cases they send preflighted 'OPTIONS' request,
which is not implemented in this 'BaseHTTP/0.3 Python/2.7.1+' server.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Walter Lee Davis  
View profile  
 More options Apr 13 2012, 8:28 am
From: Walter Lee Davis <wa...@wdstudio.com>
Date: Fri, 13 Apr 2012 08:28:35 -0400
Local: Fri, Apr 13 2012 8:28 am
Subject: Re: [Proto-Scripty] Re: Ajax.Request fails, while vanilla XHR works fine
Thanks very much for the added detail. I filed an issue on the Github project for the Python server, and within hours another Githubber had posted a pull request fixing the bug. The server was just not aware of what to do if it got such an OPTIONS request, so it was falling over.

Walter

On Apr 13, 2012, at 5:26 AM, Victor wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »