I'm getting a unique nonce (i think a timestamp) appended to all and any of my js files every time I send a request to my rails web service:
I'm fairly sure the error has to do with the ajax call, because of jquery.ajax documentation which states cache:true in your ajax request will stop ajax from appending "_={timestamp}" to the get parameters.
I have set cache: true with no luck. How else can I stop these nonces from being applied?
ajax:
$.ajax({
beforeSend: function (xhr) {
xhr.setRequestHeader ('Authorization', api_key);
},
cache: true,
dataType: 'html',
type: 'GET',
url: url+'/gwsearch/ajax_search?d1='+d1_val+'&d2='+d2_val,
crossDomain: true,
success:function(result) {
$("#display").html(result);
},
error: function(result) {
$('#display').html('Unauthorized client.');
}
});
I solved this using an ajax prefilter:
$.ajaxPrefilter('script', function(options) {
options.cache = true;
});
Related
As the title says, I wanna check if this ajax method has been submitted or not and show the result in a condition.
Here is the Ajax POST code;
$.ajax({
url: "addorderInfo.php", // Url to which the request is sent
type: "POST", // Type of request to be send, called as method
data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData:false,
success: handleResult
});
And here is the condition I put but it is not working.
function handleResult(data){
if(data == 'error'){
window.location.href ='404.php';
}
else{
$( "#clearcart" ).click();
window.location.href = "ordercomplited.php";
}
}
try this
$.ajax({
url: "addorderInfo.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function (data) {
alert(data)
},
error: function (error) {
alert(error.responseText) // if your request doesn't work
}
});
There isn't sufficient code to know why is not working.
IMHO the ajax call is not handling the error. Try to edit your code as follow:
$.ajax({
url: "addorderInfo.php", // Url to which the request is sent
type: "POST", // Type of request to be send, called as method
data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData:false,
success: function(data) {
handleResult(data);
}
error: function(data) {
handleError(data);
}
});
I have setup properties in my Ajax Request not to store cache. I have also added query string as a parameter in the URL to call new Ajax request. But whenever i refresh the page i see old content on the page. I always have to hard refresh a page to see the updated content.
The request headers are getting passed to the browser correctly which i have set in the properties of ajax. But why do i always have to hard refresh to get the updated content. How can i load the new content on just a page refresh. I have pasted my Ajax below
AJAX Methods to set the properties
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
if (options.type == 'POST') {
options.url += '?' + Date.now();
}
});
$.ajaxSetup({
cache: false,
headers: {
'Cache-Control':'max-age=0, public'
},
});
My JQuery
$.ajax({
url: "/Configuration/TestPlan/GetUserList",
type: 'POST',
cache: false,
dataType: 'json',
// async :false,
beforeSend: function () {
//call pageloader
showImageLoader();
},
data:jsonString,
contentType: "application/json; charset=utf-8",
success: function (data) {
From jquery - ajax - "If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests" -
So I don't think the cache option is doing what you think. Since it's a POST the only thing that will affect the cache-busting is that little snippet you have:
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
if (options.type == 'POST') {
options.url += '?' + Date.now();
}
});
Every time it makes a request - that page is actually being cached. It just does a new Date.now() - then that url is cahced until you do a new Date.now().
So when you comment it out - the last page/data called for is cached and now you've commented out the only thing updating the cachebuster.
Perhaps just apply the cache busting directly to the raw call:
$.ajax({
url: "/Configuration/TestPlan/GetUserList?" + Date.now(),
type: 'POST',
cache: false,
dataType: 'json',
// async :false,
beforeSend: function () {
//call pageloader
showImageLoader();
},
data:jsonString,
contentType: "application/json; charset=utf-8",
success: function (data) {
I have this code
but it run on localhost successfully but when I tried it on remote it refused
var parameters = { domainGuid:"test1",type:"testtype" };
var url = "http://remote_server/api/controller_name/test_api";
// jQuery.support.cors = true;
$.ajax({
url: url,
type: 'POST',
data: parameters,
async: false,
dataType: "json",
crossDomain: true,
success: get_all_videos_success,
error: get_all_videos_error
});
it go to error function every time
Have you tried switching your dataType from json to jsonp?
I have the following code:
$("form").submit(function()
{
//Checking data here:
$("input").each(function(i, obj)
{
});
alert(JSON.stringify($(this).serializeArray()));
var url='http://127.0.0.1:1337/receive';
$.ajax({
url: url,
type: 'POST',
contentType:'application/json',
data: JSON.stringify($(this).serializeArray()),
dataType:'json'
});
});
And after I submit the form, I get a JavaScript alert with the json string, so that is made correct (on my server I only log it so it does not matter what it is in it). If I try to send a request to the same link from postman it works, it logs it.
I think I'm doing something wrong in the ajax call, but I am not able to figure out.
Try below piece of code. Add success and error handler for more details
$("form").submit(function()
{
//Checking data here:
$("input").each(function(i, obj)
{
});
alert(JSON.stringify($(this).serializeArray()));
var url='http://127.0.0.1:1337/receive';
$.ajax({
url: url,
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify($(this).serializeArray()),
dataType:'json',
success : function(response) {
alert("success");
},
error: function (xhr, status, error) {
alert(error);
}
});
});
data:{ list : JSON.stringify($(this).serializeArray())}
From the Jquery docs:
Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.
crossDomain attribute simply force the request to be cross-domain. dataType is jsonp and there is a parameter added to the url.
$.ajax({
url:'http://127.0.0.1:1337/receive',
data:{ apikey: 'secret-key-or-any-other-parameter-in-json-format' },
dataType:'jsonp',
crossDomain: 'true',
success:function (data) {alert(data.first_name);}
});
I want to set a cookie value on an AJAX request but the code below doesn't work.
$.ajax({
type: "GET",
url: "http://example.com",
cache: false,
setCookies: "lkfh89asdhjahska7al446dfg5kgfbfgdhfdbfgcvbcbc dfskljvdfhpl",
crossDomain: true,
dataType: 'json',
success: function (data) {
alert(data);
});
How can I set cookies in the header?
Basically, ajax request as well as synchronous request sends your document cookies automatically. So, you need to set your cookie to document, not to request. However, your request is cross-domain, and things became more complicated. Basing on this answer, additionally to set document cookie, you should allow its sending to cross-domain environment:
type: "GET",
url: "http://example.com",
cache: false,
// NO setCookies option available, set cookie to document
//setCookies: "lkfh89asdhjahska7al446dfg5kgfbfgdhfdbfgcvbcbc dfskljvdfhpl",
crossDomain: true,
dataType: 'json',
xhrFields: {
withCredentials: true
},
success: function (data) {
alert(data);
});