5

I'd like a post request to be sent once a certain text input field is changed, using javascript.

So here is my current code:

<input name="message" onchange="$.ajax({type: \"POST\", url: \"http://example.com/example.php\", data: \"message=\" + document.getElementsByName(\"message\")[0].value});" />

Now, it's working on a regular connection, but it's not working on a secured connection (SSL). I mean, the page is secured, but the request is sent to a non secured page.

Is there a solution?

11
  • 3
    Sending is not a problem. Only receiving. Commented Feb 26, 2011 at 21:19
  • very true, but i assume since he's doing ajax he's also looking to receive? Commented Feb 26, 2011 at 21:19
  • @kjy112: That is very likely, but not stated in the question (yet). ;) Commented Feb 26, 2011 at 21:21
  • @FelixKling you were right. assumption is never a good thing =/ Commented Feb 26, 2011 at 21:53
  • 1
    How do you submit the form? As Ajax or as "normal" request? If you do a normal request, i.e. the browser is redirected to a new (or the same) site, then making this call to the other domain is better done at the server side. Commented Feb 26, 2011 at 22:01

2 Answers 2

2

Set the target attribute of the form to point to a hidden iframe.

You won't be able to read the response, but you can make the request.

If you want to read the response, you will need to proxy the request through your own server.

Sign up to request clarification or add additional context in comments.

4 Comments

That's not what I meant. I want the form to be sent normally, and in the meanwhile make an ajax post request to another domain (just send it, I don't need to read the response).
@Lior so you basically want to send the form at the sametime do post to another domain? Something like php's cURL?
The submit the form, give it some time to go through, then change the target of the form and call its submit() method.
that's abit risky though. i would think server-side solution is better serve
0

As David points out, t is not possible to do an asynchronous POST to a service on another domain, due to the (quite sensible) limitation of the same origin policy. JSON-P only works because you're allowed to insert tags into the DOM, and they can point anywhere.

YOu can do cross-domain AJAX w/ GET using JSONP: JSONP CrossDomain

It also covers how to do JSONP w/ jQuery

same origin policy prevents a document or script loaded from one origin from getting or setting properties of a document from another origin. Two pages are considered to have the same origin if the protocol, port, and host are the same for both pages. http://rj3.net/mdc/sop

make sure you specify ssl in the ajax url, when the rest of your page uses ssl too e.g https

You can't send it from a https to a http site. any https asset (html or otherwise) can only be accessed by something on the same domain / ssl certificate. so, you won't be able to do what you are trying to do (https to http). so since your page is served on https the targeted http site can not access it due to the policy

8 Comments

Everything that page has to say about using JSON-P does it using the GET method. The question asked for POST.
@David Dorward: Well, on the one hand, the server must support JSONP anyway. And on the other hand, nothing speaks against using JSONP with a POST request. Only because the article uses GET in its examples does not mean it is not possible to use POST.
Actually, that is exactly what it means. JSON-P works by having a script on a third party site (imported with <script src>) call a function defined by the first party site. There is no way to import a script using a POST request (unless you do something like XHR it and then eval it — but that can't reach the third party site).
totally didn't see OP is doing strictly POST sorry.
@David Dorward: Ah sorry. I had a slightly wrong idea about JSONP. Of course the retrieval of the script cannot be done via GET. Somehow I thought there is another request involved and I meant this to be a POST request... no hard feelings!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.