In my Javascript heavy Web app, I have some scripts that are loaded in the source HTML with tags, and others that I load as needed using jQuery's ajax method.
I am in the process of relocating most of my app's static assets to a CDN (I'm using AWS Cloudfront). My dynamic loads no longer work. The jQuery Ajax call:
$.ajax({
url: url,
dataType: 'script',
async: true,
success: function (content) {
....
triggers the success function, but the content is empty.
I'm wondering if this is because such a request violates the Browser's security model for cross-site scripting. If that's the case, is there to load scripts dynamically from a CDN? Ahhh... I am NOT presently using a CNAME alias for the cloudfront host. Would doing so solve this problem?
You're right, it is the cross-site scripting security preventing this.
You're able to use the jQuery.getScript() method to load it from an external domain (or internal).
Related
I have created REST API in codeigniter. This API I am using on my codeigniter website and It's working fine. I have created one JavaScript for call API using ajax. It's also working fine but problem is that after calling ajax If refresh the page or tired to open another page of website it's send to me login page. I don't know why my session is expired ?
I have check in chrome developer tools. After calling ajax clicked page return 302 status.
$.ajax({
url: rest_url,
dataType: 'jsonp',
success: function(data){
//some code
}
});
This problem is caused by session ID regeneration during AJAX requests
If you upgrade your codeigniter , it will be good because we have fixed this error with codeigniter. If you can't , you can get a view in how we have fixed it here Fix of this bug in github. As you can see you just need to change the if condition on this file system/libraries/Session.php. I think that my answer will be usefull for you.
I'm having a problem with our ASP.NET MVC4 web application:
Our web application is used for realtime data visualization of values and parameters of different industrial devices. When a typical monitoring page gets opened in a client's browser, the page itself is static at first. When the document has finished loading, the current values for the datapoints on this page get loaded via an ajax call from the database. The call returns JSON data that feeds the viewmodel, which is then used by Knockout.js to update the UI elements with the data.
After this initial loading of current data, any upcoming changes to any of the currently displayed datapoints are transmitted from the server to the page via the WebSocket protocol and are then put into the viewmodel, replacing the old values. The new data also gets immediately written to the database, so that an ajax call would always deliver the up-to-date values.
So far, everything works really stable and fast in all HTML5-capable browsers - except for Internet Explorer 10. I'd like to add: OF COURSE ;-)
The problem in detail:
When I open a specific page with some datapoints, it loads the page. The current values get correctly fetched from the DB with ajax. Then, new values arrive and they are shown correctly with the mechanisms described above. The values in the UI get updated.
Now I change to another page, wait for it to get loaded completely, and then I go back to the first page (it doesn't matter if I do this with the "Go Back" button or by clicking a link or entering the URL manually), the page does NOT display the current values, but the values that had been loaded initially in step one via ajax.
The confusing thing is: I verified that the database DOES contain the up-to-date values and not the values shown on the page, therefore they should have get loaded with the initial ajax call when returning to the first page. The annoying thing: they DO get loaded in Firefox, Chrome, and even IE9 etc - but not in IE10.
Is there some new caching mechanism in IE10? How can I resolve this issue? Besides that: as far as I can see, it looks like there's some kind of Ajax / JSON caching going on here, are there ways to definitely prohibit HTML and JSON caching completely / globally in my ASP.NET MVC4 project or IIS?
Thanks for your help!
The jQuery AJAX method allows you to turn caching off:
$.ajax({
url: "test.html",
data: 'foo',
success: function(){
alert('bar');
},
cache: false
});
If this does not solve you can use the querystring trick:
$.ajax({
url: "test.html"+'?ts=' + $.now(),
...
cache: false
});
Anyway the cache: false will do the trick. If not maybe you are using an old jquery version and it is better to upgrade.
I have a mobile version of a website built in jQuery mobile and living on a dedicated "m.myurl" subdomain. The full desktop version of the site is in Wordpress. My goal is to have updates entered via Wordpress to the desktop site to show up dynamically on the mobile site as well. I am generating the page content from the individual pages of the desktop site via JSON feeds, and have confirmed that the JSON feeds are good (i.e.: if I enter their URL into a browser, I get a good JSON object). However, the AJAX call on the jQuery mobile site refuses to work. I have debugged to the point that I am not receiving any errors in my console, but when I attempt to log the response from the AJAX call to troubleshoot, it consistently shows in the log as undefined, so I am not receiving known good JSON objects.
The functions I'm using to make the AJAX call are as follows:
function processResponse(response){
var update = $(response).find('content');
$('.content').append($(update).html());
};//ends processResponse
function processJSON(url){
$.ajax({
url: url,
data: null,
success: processResponse,
dataType: 'jsonp',
});//ends ajax
};//ends processJSON
I've set the three JSON URL's equal to variables, but have tried calling the function with the URL passed in instead of the variable, to no effect. The 'processJSON" function is called within the content div of each page section of the jQuery mobile index.html file within a $(document).ready.I'm using 'jsonp' as the dataType, as I assume that calling from a subdomain to a main-level domain would be considered a cross-domain call. That said, I'm trying to get this to work locally, and haven't placed it on a test server yet.
Any assistance is appreciated. Thanks!
I am using MVC . My scenario is like i need to post file and forms variables to controller.
I tried AjaxSubmit which works in all browsers except for IE. It shows "access is denied".
$(".WW_Register_Form").ajaxSubmit({ url: url, type: 'post', enctype: 'multipart/form-data'});
My requirements are to post a file and post form parameters in ajax.
Is there any other jquery plugin by which i can accomplish this?
You are probably receiving the access denied error due to restrictions on XMLHttpRequests over cross-domains.
Are you trying to access another domain that isn't of same-origin? Even something as simple as removing the www off of www.formsubmit.com could cause an issue with your requests and the browser does not see this as a same-origin
Have a look here https://developer.mozilla.org/en-US/docs/JavaScript/Same_origin_policy_for_JavaScript and here http://en.wikipedia.org/wiki/Same_origin_policy for clarification.
My suggestion is to make sure that you are trying to contact an application/script that is of same-origin.
I am trying to get getJSON working on my IE, but I read that IE doesn't support it that well. So I decided to use ajax instead.
My code is:
$.ajax({
dataType: 'json',
url: 'http://openexchangerates.org/latest.json',
success: function(data)
{
alert('done');
}
});
Thanks
You cannot make crossdomain ajax calls. Take a look at the top post at this SO post. If you have a specific need to do so, you need to use some sort of sockets. This can be file_get_contents in php or another server-side language
I had this problem just the other day. Internet Explorer doesn't like you doing cross-domain requests with AJAX.
I solved it by having a server-side script do the cross-domain API request, and then call the result of that script in my AJAX call.