jQuery Ajax caching - caching

I make a few Ajax calls to get files via jQuery like so:
$.ajax({
url: "/resx.mvc",
data: {
virtualPath: options.virtualPath,
keys: options.keys,
global: options.global
},
cache: true,
success: function (values) {
$.extend(assignTo, values);
},
dataType: "JSON",
traditional: true
});
When I look at the request in Fiddler, I see that these two headers are being sent, and making my ASP.NET send back an expires header on its response with -1:
Pragma: no-cache
Cache-Control: no-cache
How do I tell jQuery not to issue no-cache?

beforeSend takes an ajax object (XmlHttpRequest object) which you can use to manipulate the request header. Below is an example of setting a header in your request using the ajax object returned in the callback:
$.ajax({
type:"POST",
beforeSend: function (request)
{
request.setRequestHeader("Authority", authorizationToken);
},
url: "entities",
data: "json=" + escape(JSON.stringify(createRequestObject)),
processData: false,
success: function(msg) {
$("#results").append("The result =" + StringifyPretty(msg));
}
});

Related

Add bearer token to ajax POST request in ASP.Net Core

I have a POST method /Home/PostRequest decorated with the [Authorize] attribute in ASP.Net Core. Client-side I have an authenticated user. I want to construct an ajax POST request to call the method, but I'm unsure what to put into the Authorization header. Where are the auth credentials cached, and in what form?
$.ajax({
type: "POST",
url: "/Home/PostRequest",
data: JSON.stringify(request),
headers: {
"Authorization": "** what to put here? **"
}
contentType: "application/json",
dataType: "json",
crossDomain: false,
success: function (response) {
successCallback(response);
},
failure: function (response) {
console.log("Failed");
},
error: function (xhr, response, thrownError) {
console.log("Error");
}
});

Ajax post request can't get data with express router

I'm using express.Router() to manage my routes.
then using Ajax post request send some data to it. but I can't get the data.
ajax code :
$.ajax({
url: '/custom',
type: 'POST',
contentType: 'application/json; charset-utf-8',
dataType: 'json',
processData: false,
data: JSON.stringify({ "key": "values" }),
success: function(data) {
console.log(data.toString());
}
})
express code:
var express = require('express');
var router = express.Router();
router.post('/custom', function(req, res) {
console.log(req.body);
console.log(req.params);
console.log(req.query);
res.write(JSON.stringify());
res.end();
});
but all the logs are empty.
The issue is because the content-type header in your ajax is malformed.
Instead of
contentType: 'application/json; charset-utf-8' it should be contentType: 'application/json; charset=utf-8'.
Assuming you are using the bodyParser.json() middleware, this would cause the body to not be parsed, which would print an empty body. When you sent the requests with Postman the proper headers were sent, which is why it worked there but not from the ajax request.

Does JSONP response need callback in response

I'm not clear if my response to a JSONP call needs to have the callback reference in the response. For example, the following AJAX call:
$.ajax({
type: 'GET',
url: ajaxurl ,
async: false,
dataType: "jsonp",
jsonpCallback: "do_teacher_survey_callback",
data: {action: 'test'},
success: function (result) {
},
error: function (request,error) {
}
});
Does the response need to look like the following:
do_teacher_survey_callback({"field":"data"})
Or can I just return pure JSON like this:
{"field":"data"}
I'm confused because I have used JSONP before to resolve cross-domain calls to servers that I had no control over the response and it worked fine.

jquery ajax success not working

I got simple ajax call i could not get why it is not running success method despite the fact that chrome developer tools show that it is getting the response for the request.
$( document ).ready(function() {
var url_API="http://moviesapi.herokuapp.com/cinemas/find/"+"PL15RH";
$.ajax({
type: 'GET',
url: url_API,
dataType: "jsonp",
crossDomain:true,
success: function (response) {
alert(1);
}
});
});
The API doesn't support jsonp. You're getting 500 (Internal Server Error).
It does support JSON, but you're getting the classic No 'Access-Control-Allow-Origin' header is present on the requested resource CORS error. You need to explicitly send the Access-Control-Allow-Origin header on your heroku API:
Header property:
Access-Control-Allow-Origin: *
A more verbose solution: "No 'Access-Control-Allow-Origin' header is present on the requested resource"
try this:
jQuery(document).ready(function($) {
var url_API="http://moviesapi.herokuapp.com/cinemas/find/"+"PL15RH";
$.ajax({
type: 'GET',
url: url_API,
dataType: "json",
crossDomain:true,
success: function (response) {
alert(response);
}
});
});

jQuery Ajax POST changes Content-Type in Firefox

I'm making an Ajax call to POST a collection of fields, objects, and arrays to PHP. 9 times out of 10 this works just fine. But when submitting the same request a number of times (no changes to the data) the request will occasionally be sent with Content-Type: text/plain or text/html rather than application/x-www-form-urlencoded, causing issues in my PHP. JSON might be more advisable, but can anyone think of why this could be happening?
This is not a cross domain request.
This only happens in Firefox.
I don't think I need to specify the Content-Type since Ajax will default.
I explicitly call $.param() but don't think I need to.
I expect JSON in return.
$.ajax({url: action,
type: "POST",
dataType: "json",
data: $.param($(domElement).data()),
complete: function() {
// Cleanup
},
success: function(data) {
// Handle success.
},
error: function() {
// Handle error
}
});
Not sure why this only happens in Firefox, but does it help if you set the content type manually?
$.ajax({url: action,
type: "POST",
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
dataType: "json",
data: $.param($(domElement).data()),
complete: function() {
// Cleanup
},
success: function(data) {
// Handle success.
},
error: function() {
// Handle error
}
});

Resources