Jump to content

Ajax cross server issue with "Ajax Upload" script


schilly

Recommended Posts

Hey,

 

I'm running into some weird issues doing cross server ajax calls with http://valums.com/ajax-upload/. The request works fine and the file uploads and saves properly but for some reason the xhr response is invalid so I get an error every time even though the json success message is returned.

 

PHP Request Processor:

<?php

//check for cross server access check
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS' && $_SERVER['HTTP_ORIGIN'] == 'http://domain1'){
header('Access-Control-Allow-Origin: http://domain1');  
header('Access-Control-Allow-Methods: POST, GET');  
header('Access-Control-Allow-Headers: X-Requested-With,X-File-Name');  
header('Access-Control-Allow-Credentials: true');  
header('Access-Control-Max-Age: 1728000');  
header("Content-Length: 0");  
header("Content-Type: text/plain");  
exit();

} else {
//return success message
echo htmlspecialchars(json_encode(array('success'=>true)), ENT_NOQUOTES);
exit();

}

?>

 

Request Headers:

 

http://domain2/scripts/ajax/processUpload.php?pid=1&qqfile=5weeks-copy.jpg

 

OPTIONS /scripts/ajax/processUpload.php?pid=1&qqfile=5weeks-copy.jpg HTTP/1.1

Host: domain2

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 115

Connection: keep-alive

Origin: http://domain1

Access-Control-Request-Method: POST

Access-Control-Request-Headers: x-file-name,x-requested-with

 

HTTP/1.1 200 OK

Date: Thu, 09 Dec 2010 00:34:04 GMT

Server: Apache/2.0.52 (Red Hat)

X-Powered-By: PHP/5.2.14

Access-Control-Allow-Origin: http://domain1

Access-Control-Allow-Methods: POST, GET

Access-Control-Allow-Headers: X-Requested-With,X-File-Name

Access-Control-Allow-Credentials: true

Access-Control-Max-Age: 1728000

Vary: Accept-Encoding,User-Agent

Content-Encoding: gzip

Cache-Control: max-age=259200

Expires: Sun, 12 Dec 2010 00:34:04 GMT

Content-Length: 20

Connection: close

Content-Type: text/plain

 

http://domain2/scripts/ajax/processUpload.php?pid=1&qqfile=5weeks-copy.jpg

 

POST /scripts/ajax/processUpload.php?pid=1&qqfile=5weeks-copy.jpg HTTP/1.1

Host: domain2

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 115

Connection: keep-alive

X-Requested-With: XMLHttpRequest

X-File-Name: 5weeks-copy.jpg

Content-Type: application/octet-stream

Referer: http://domain1/folder/pilothouse/test-upload.php

Content-Length: 12154

Origin: http://domain1

Pragma: no-cache

Cache-Control: no-cache

ÿØÿà

 

HTTP/1.1 200 OK

Date: Thu, 09 Dec 2010 00:27:42 GMT

Server: Apache/2.0.52 (Red Hat)

X-Powered-By: PHP/5.2.14

Vary: Accept-Encoding,User-Agent

Content-Encoding: gzip

Cache-Control: max-age=259200

Expires: Sun, 12 Dec 2010 00:27:42 GMT

Content-Length: 36

Connection: close

Content-Type: text/html

 

Yet when I look at the xhr response, the readyState is 4 meaning it's done but the status is 0 and there is no responseText. Anyone have any ideas what I can try? The response works fine if I do it on the same server.

 

I'm guessing it has something to do with the headers but I'm really not sure. It seems like everything other than the xhr readyState is null.

 

Anyone have anything I could try?

 

Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.