post data to another domain using ajax - javascript

I need to post some json data to external(another domain) API using ajax which on success should return me back some json data.
This API doesn't supports GET, only POST and I have no control on it, which means I can't do JSONP or enable CORS.
Any idea how to bypass the cross-domain limitations?

Post the data to your own server. Make the HTTP request to the API from your server. Relay the response.

You have to use a proxy page: you'll send the ajax post to the proxy page, which must reside on the same domain, and the proxy page will take care of posting the data to the final destination.
A php example: http://jquery-howto.blogspot.it/2009/04/cross-domain-ajax-querying-with-jquery.html
A Java example: http://snipplr.com/view/17987/

Related

How to send DELETE request using browser in javascript

I have to use Postman to send POST PUT and DELETE request to my server, is there any way I can hit POST request through browsers directly, using browser front end only
I Have a form that on submits should delete data from my server but using a form I can send only the GET or POST method only I know I can do it with the POST method, but I need to do it by DELETE.
You may be able to send a "DELETE" request via Ajax, the question should be will your server understand it.
While DELETE is a http method, it's not a generally supported request type in most back-end frameworks, rather you generally "overload" a POST request while setting the action as delete via a hidden form field.
So depending on your back-end framework, you would need to consult their documentation, but generally its accomplished as described above.
As for performing a POST request by URL alone, in a browser there is no way that I'm aware of, you need some tool, to be able to set headers to be able to form a POST request, such as JavaScript, curl etc
MDN has a great introduction to the topic
The HTTP POST method sends data to the server. The type of the body of the request is indicated by the Content-Type header.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST

How to make Cross Domain Request to an API from Javascript Client when I don't have access to modify the other domain API?

I have a scenario where I need to fetch JSON response data from an API in a different domain. Lets say the requesting domain is ClientDomain and the API domain in APIDomain.
Now I know that this can be achieved using Access-Control-Allow-Origin header in the API server and then using a client ajax marking the CrossDomain attribute as true, etc.
All my search leads either to modify the API settings to add the Access-Control-Allow-Origin header or do a proxy coding in backend server code. But in my case, I don't have access to the API code and I can't change anything there. I'm able to do a proxy read from C# controller code and fetch data from the APIDomain. I want to know how this can be achieved only with Javascript/Jquery in my current scenario.
Edit: Is there a way to do proxy-ing via Javascript/Jquery?
Any help would be great!
Make an API/page on ClientDomain and access it via Javascript/Jquery. In that API/page call the API on APIDomain and return its data.

Why won't $.getJson work with json or jsonp?

I am trying to pull information from Google Finances stock screener. There is no official api, so I am just making a get request to the same URL that they use on the site. I am using the URL at the bottom of the question, it can get a bit long. I can go to the url myself and it will give me a text file with the JSON information. In my javascript I am using $.getJSON on the url to get the screener results. But I get a Access-Control-Allow-Origin error, so I change output=json to output=jsonp&callback=?. But it only returns ();. From what I can tell this means that it is not set up on the other end to respond to a jsonp request and cannot return the proper information.
I have also tried output=json&callback=?, which produces the (); and output=json&callback=callbackFunction and output=json&callback=callbackFunction which both give me Access-Control-Allow-Origin.
Is there any way that I can make this work?
https://www.google.com/finance?output=json&start=0&num=20&noIL=1&q=[currency%20%3D%3D%20%22USD%22%20%26%20%28%28exchange%20%3D%3D%20%22OTCMKTS%22%29%20%7C%20%28exchange%20%3D%3D%20%22OTCBB%22%29%20%7C%20%28exchange%20%3D%3D%20%22NYSEMKT%22%29%20%7C%20%28exchange%20%3D%3D%20%22NYSEARCA%22%29%20%7C%20%28exchange%20%3D%3D%20%22NYSE%22%29%20%7C%20%28exchange%20%3D%3D%20%22NASDAQ%22%29%29%20%26%20%28market_cap%20%3E%3D%200%29%20%26%20%28market_cap%20%3C%3D%200.1%29]&restype=company&ei=GLyhVKmcDpOb8gbm7IGQAQ
If the service doesn't provide a JSONP endpoint or use CORS to grant you permission to access some other kind of endpoint, then there is no way to access the data using client side code.
Use server side code instead. You can use that to present the data to your client side code.

Making post request from redirection in node.js

I am trying to redirect to another URL from node js by using response.writeHead method
response.writeHead(301, {Location : <redirecturl>})
I need to have this redirection is being executed by POST method, however it is always executed by GET. What is the way that the redirection can be made as a POST request.
A redirect by default sends a GET request. According to the HTTP spec, a 301 redirection means sending a GET request. If you want to redirect with the same method (POST), you can try doing a 307 redirect instead.
There is no difference between redirection in both GET and POST methods. Both method should work find. Better you can your expressjs framework in which it is
res.redirect('http://example.com');
Be careful, when using status code 301 which means Moved Perman­ently. Once browser gets 301 status code, it will directly try the redirected URL from the next time.
Without seeing more of your code, I believe this is what you are describing:
The client has made a request to your application using an HTTP method (get, post, etc.) You are responding to that request by sending back a 301 error and a new URL (redirecturl)
The client then decides to implement a get request for the redirecturl.
You can't change how a client responds to a 301. That is out of your control and it is normal for browsers to initiate a get for the redirecturl.
You could initiate a post request from your server to the redirecturl.
You could send back a webpage which would then submit a post request from the client.
What are you trying to achieve?

using oembed - json responses (in client side javascript)

If an oembed provider is only outputting json and xml, does that mean it is impossible to use this via client side javascript? ($.ajax request to the provider)?
Can we only use oembed providers that allow for a jsonp callback in javascript?
Thanks,
Wesley
you cannot use ajax across different domains.
Jsonp is an option, other option is to have a server side script which will fetch the data from the provider and then make an ajax call to that script to return the fetched data

Categories

Resources