1

I am passing an encoded json array in a ajax GET request (it's cross domain) but I am getting a 404, as it's parametrising the data in the url and is too long.

What's the better approach to sending data to a webserver using javascript over cross domain? It's a fairly small amount of data <1000 variables with only around 50 chars per variable.

ShiftyThomas
  • 476
  • 5
  • 18

2 Answers2

1

There's no limit on the body of a normal HTML POST request.

JavaScript post request like a form submit

Community
  • 1
  • 1
Paul Collingwood
  • 9,053
  • 3
  • 23
  • 36
  • 2
    post is the solution. and being a cross domain request doesn't prevent you to use it! – Samuele Mattiuzzo Jan 04 '13 at 11:17
  • @Paul C How would I handle the response using that function in the above question? The webservice would return a JSON string. – ShiftyThomas Jan 04 '13 at 11:54
  • good question. I don't know offhand, but this discussion seems relevant. http://stackoverflow.com/questions/374644/how-do-i-capture-response-of-form-submit or you can just go back to where you started from and send the data in batches until complete. – Paul Collingwood Jan 04 '13 at 12:03
  • I still have not really found a solutions for this. It seems very odd that there is only a 'hackish' way of doing this. REST Webservices are very common and I would have thought someone would of had to send data to an external webservice when the data is larger than an url can allow. – ShiftyThomas Jan 07 '13 at 11:29
  • 1
    Ok this is what I did, I used the form post in this answer but also posted a unique id with data, so I could poll the server to find the status of the post data. I don't really like it and it's a bit messy but it's the only way I could find :( – ShiftyThomas Jan 09 '13 at 10:58
0

Please used post method instead of get method.... Try jsonp in your ajax since you are accessing cross domain with call back function.For more information about jsonp see the link:- http://www.cypressnorth.com/blog/programming/cross-domain-ajax-request-with-json-response-for-iefirefoxchrome-safari-jquery/

http://json-p.org/

The most important thing you need is the server side page you are using for doing curl has to set some headers for allowing http to https connection. This are below....

header("Access-Control-Allow-Origin: your https url");
header("Access-Control-Allow-Methods: POST, GET");
header("Access-Control-Max-Age: 1728000");

header("Access-Control-Allow-Headers: Content-Type, Connection, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control");
header("Connection: close");
THE ONLY ONE
  • 2,110
  • 1
  • 12
  • 6
  • I tried this but could not get it to work, I would also have to allow origin * as the post url could be anything. The code does not reside on a server so I can't use curl to post, I'd also get the same issue with jsonp as i am limited in url size if I parametrize the data. – ShiftyThomas Jan 09 '13 at 11:05