jquery Post: request uri too long
So here's an interesting problem. I'm posting to a web service and I'm getting the request URI is too long. jQuery seems to be appending my data to the URL:
// this is dynamically generated, and can get very long
var s = 'q=string1&q=string2';
$.ajax({
type: 'POST',
url: 'https:开发者_如何学编程//www.googleapis.com/language/translate/v2?',
data: s,
dataType: 'jsonp'
});
I also tried:
data: {data: s}
Same thing.
The tricky part is that I need to translate multiple items. Google requires that you pass the 'q' variable multiple times. Not an array, but the same parameter multiple times. Which seems stange. So that eliminates using a json object, because you can't have the same key twice. Hence the reason I have to pass a string.
Any idea how I can accomplish this and get the data in the request body instead of the URI?
Thanks.
There is no such thing as a JSONP POST request. JSONP is about passing the parameters in the src
attribute of a script tag, and script are always loaded with GET. If you set the data type of the $.ajax
call to jsonp
, jQuery will disregard the type
setting and use GET (where URI length limits apply). Sending cross-domain POST requests with AJAX is possible but rather complicated and requires the cooperation of the target server. Otherwise, you need to use some sort of proxy server.
The answer is that you can't post to remote URIs. Apparently this is not allowed through jQuery. I had to post to my own server, then and pass the call through accordingly. jQuery will try to convert the request to a GET request, because that is allowed.
I notice that you have set: X-HTTP-Method-Override
to GET
,
which might be a probable reason for such a behavior.
What essentially X-HTTP-Method-Override
does is to tell the Restful clients to override the actual HTTP method and interpret the request as what X-HTTP-Method-Override
is set to be.
Therefore, the request might actually be working as GET
request.
Try changing it to POST
and see what happens.
精彩评论