So I am trying to retrieve an rss feed in a synchronous call like so:
var oReq = new XMLHttpRequest();
oReq.open("GET", topicsUrl, false);
oReq.setRequestHeader("Access-Control-Allow-Origin", "*");
oReq.send();
The Send throws a Network Error, but did at one stage work. This is a cross domain xhr request that needs to be synchronous. I have tried to use the onreadystatechange between the header and the send but it didn't help. If I use an Asynchronous call the return data parsing starts before there is any data returned? So I'm a little puzzled how I can get this to work
The server needs to respond to your request with the Access-Control-Allow-Origin: * header. You sending it from the client does nothing.
Related
I'm making requests to Google Time Zone API using Ajax and by following this tutorial:
var apicall = 'https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810,-119.6822510×tamp=1331161200&key=YOUR_API_KEY'
var xhr = new XMLHttpRequest() // create new XMLHttpRequest2 object
xhr.open('GET', apicall) // open GET request
xhr.onload = function(){
if (xhr.status === 200){ // if Ajax request successful
var output = JSON.parse(xhr.responseText) // convert returned JSON string to JSON object
console.log(output.status) // log API return status for debugging purposes
if (output.status == 'OK'){ // if API reports everything was returned successfully
// do something
}
}
else{
alert('Request failed. Returned status of ' + xhr.status)
}
}
xhr.send() // send request
I've replaced the key parameter with my generated API Key in Google Console, and the request works. The problem is I can't seem to restrict the API key access by either Referrer or Server IP in Google Console- specifying the domain of my server or IP doesn't work. My guess is Ajax doesn't send referrer or server IP info along with the request for Google to determine if it's a valid request? At the moment I'm stuck with using no API key restrictions, though this is not a good idea of course.
Anyone have any experience restricting Google API key access when making calls to Google APIs via AJax?
just remind, just do not embed API keys directly in code
google api
According to the Crossrider docs, there is currently a webRequest object with the onRequest event, which allows for accessing the request URI, etc., but I could not find any way to inspect the response data. I was wondering if there is a way to actually inspect the response of that request, such as the response headers and possibly the response data. Thanks for the help.
I'm going out on a limb here but I think your after an AJAX request, in which case you are after the appAPI.request API where you can indeed obtain response and header data. The appAPI.webRequest API is like Chrome's webRequest API used "to observe and analyze traffic and to intercept, block, or modify requests in-flight".
If this is not the case, please can you clarify your scenario.
[RESPONSE TO COMMENT]
Let me first clarify that the webRequest API runs before the requests are made (i.e. the browser is requesting the page but has not yet made the actual HTTP request) and hence it's not relevant to talk of response data.
However, since the URL is provided in the details of the webRequest, you can get the response data and headers yourself using the aforementioned appAPI.request API, as follows:
appAPI.ready(function() {
appAPI.webRequest.monitor.onBeforeNavigate.addListener({
callback: function(details) {
appAPI.request.get({
url: details.requestUrl,
onSuccess: function(response, additionalInfo) {
appAPI.db.async.set(details.requestUrl, {
response: response,
headers: additionalInfo.headers
});
}
});
}
});
});
NOTE: For this example I have used the new webRequest.monitor API designed to be lightweight specifically for monitoring. It's not currently documented (docs should be completed within the next week or so) but you can start using it already for Chrome, Firefox, and Internet Explorer.
[Disclosure: I am a Crossrider employee]
My application is making a network call using xmlHttpRequest. In the response i am getting Set-Cookie header (verified with fiddler). I need to access these cookies from javasript. I tried with XmlHttpRequest.getAllResponseHeaders(), it is returning all headers except Set-Cookie.
Is there a way to access these cookies from javascript? If yes, please provide some example.
My application is running on Webbrowser control (IE10), Windows Phone 8.
Thanks in advance.
While awaiting a more specific answer, you can instead send all the cookies set from the server through a post response, then set it locally, as so (using jQuery to make it easier):
// Client
var cookie;
$.post('example.com',{'stuff':'data'},function(data){
cookie = data;
});
// Server
if(isset($_POST['stuff'])) echo WhateverTheCookieWouldBe;
Actually cookies can be accessed via
document.cookie // this will return a string contains all cookie values separated by semicolon
This is actually not true since because of the async nature of request
// Client
var cookie;
$.post('example.com',{'stuff':'data'},function(data){
cookie = data;
});
alert(cookie); // undefined
Is there a way to send a POST request using AngularJS despite the same-origin policy?
I don't need to get the response from the request, I just need to send the request.
Just like creating a form and sending it to another server.
Thanks
You can use JSONP to send a request to another domain, however you can't use POST, it would have to be a GET request. Can you serialize your form values and send using GET?
http://docs.angularjs.org/api/ng.$http#jsonp
How to use type: "POST" in jsonp ajax call
Keep in mind that if you do use GET, you are limited with how much data you send, since URLs usually can't be over ~2000 characters.
I know when you request a page normally it is typically the case that you would use server side session data and set cookies via HTTP headers, but does it work the same when the content is requested via AJAX?
An AJAX request contains the same request/response information as a traditional HTTP request. You can set cookies on the client once the async callback is executed, etc.