Cross-Origin Request Blocked happening others computer, but not mine - ajax

I have a website hosted in ColdFusion Server that do a http post to my application hosted in Azure Server. I set a CROS-Orign exception in my Azure server that allows request from the ColdFusion Server. Everything is working fine when I run it by my computer (not localhost. The coldfusion server). However, if someone else tries it using another computer, it returns:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at azureServer
Is it possible? How can it knows my computer?
My AJAX request:
data = {
var1: 10,
var2: "ABC"
};
$.ajax({
url: "https://myAzureServer",
headers: {
'Access-Control-Allow-Origin': '*'
},
type: "POST",
dataType: 'json',
data: data,
success: function(result) {
if (result) {
console.debug("Success");
} else {
console.debug("Error after running");
console.debug(result);
}
},
error: function(xhr, status, p3, p4) {
var err = "Error " + " " + status + " " + p3;
if (xhr.responseText && xhr.responseText[0] == "{")
err = JSON.parse(xhr.responseText).message;
console.debug("error");
console.debug(err);
}
});

Related

Ajax post works for empty ASP.net web app, but if same code is used for Web Forms project, it causes a 401 error

I am having an issue submitting data using the script below. The post will work fine if the Visual Studio (2019) project is an EMPTY ASP.Net Web Application, but if I use the same script for a Web Forms ASP.Net Web Application project, it gives me a 401 error.
In both cases, the web site is set for anonymous authentication, and when I look at the web logs between both projects, one difference that stands out is that on the Web Forms project that does not work, it is dropping the page extension. Could this be causing iis to try to access a folder (which does not exist) instead of the file? If so, what is causing the extension to drop and how do I fix this?
IIS log for Web Forms Project (No Good)
2020-06-23 15:14:34 ::1 POST /final.aspx/ChangeDescription - 44391 - ::1 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/83.0.4103.106+Safari/537.36+Edg/83.0.478.54 https://localhost:44391/final 401 0 0 157
IIS log for Empty Project (Good)
2020-06-23 15:09:42 ::1 POST /final.aspx/ChangeDescription - 44361 - ::1 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/83.0.4103.106+Safari/537.36+Edg/83.0.478.54 https://localhost:44361/final.aspx 200 0 0 124
<script>
$("#NewDesc_SaveButton").click(function (e) {
if ($("#NewDesc_TextBox").val() == "") {
$("#NewDesc_Span").text("Enter New Description");
}
else {
$("#NewDesc_Span").text("");
}
if ($("#NewDesc_TextBox").val() != "")
$.ajax({
type: "POST", url: "final.aspx/ChangeDescription", contentType: "application/json; charset=utf-8", data: '{"NewDescription":"' + $("#NewDesc_TextBox").val() + '"}', dataType: "json", success: function (result, status, xhr) {
if (result.d == "Success") { $("#StatusMsgSpan").text("New Description Has Been Saved."); setTimeout(function () { $('#ChangeDesc_Modal').modal('hide'); }, 2000); }
else $("#StatusMsgSpan").text("New Description Not Saved.");
}, error: function (xhr, status, error) { $("#dbData").html("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText) }
});
});
</script>
With the assistance of a colleague, it turns out I needed to change settings.AutoRedirectMode = RedirectMode.Permanent to settings.AutoRedirectMode = RedirectMode.Off in the App_Start RouteConfig.cs file.
Make sure you clear the browser cache before you test the change.

Ajax call to another website

For some reason when I run this simple javascript snippet I get the message "Error! Status = 404 Message = error"
callPHP(1)
function callPHP(args)
{
$.ajax({
url: "http://lectiogroupgenerator.esy.es/index.php",
type: 'post',
data: { "json" : JSON.stringify(args) },
dataType: "json",
success: function (data)
{
if (data)
{
alert(data);
return data;
}
else
{
alert("Data is empty");
}
},
error: function (xhr)
{
alert('Error! Status = ' + xhr.status + " Message = " + xhr.statusText);
}
});
return false;
}
My PHP file is just:
<?php
?>
I'm guessing 404 implicates that the php could not be found, but it does exist and I have no clue why it can't find it maybe it has something to do with me making a google chrome extension?
It might be because of the CORS issue. http://lectiogroupgenerator.esy.es/index.php doesn't allow cross origin HTTP requests.
If that's not the case try explicitly defining the website in the permissions in the manifest file to allow requests in and out to that website.
The problem was caused by the Same-origin policy, it was solved when I got an SSL certificate for my website.

CORS is driving me crazy - intermittent issues

It seems like I've tried everything and I finally just switched to using the CORS npm module:
var cors = require('cors');
And my one route I want to use CORS on:
app.post('/hangouts', cors(), hangoutsController.hangouts); // user CORS
I'm implementing a custom app in Google Hangouts, but need to post to my server, and the Hangout is run from a Google server. I put the AJAX call on a loop so that it will keep trying - this post going through is crucial to my app.
Here's the relevant AJAX call in the Hangout app:
var shouldpostHangoutId = true;
/* Post the Hangout ID to server */
var postHangoutId = function(hangoutId) {
var startData = gapi.hangout.getStartData();
$.ajax({
type: 'POST',
url: rootURL + "/hangouts",
crossDomain: true,
dataType: "json",
data: {
"hangouts_id" : hangoutId,
"start_data" : startData
},
success: function( response ) {
console.log( "postHangoutId -- success" ); // server response
console.log( response ); // server response
shouldpostHangoutId = false;
},
error: function(xhr, textStatus, error){
console.log( "postHangoutId -- error" ); // server response
console.log(xhr.statusText);
console.log(textStatus);
console.log("error = " + error);
// Try again
if (shouldpostHangoutId) {
postHangoutId(hangoutId); // Try again
};
}
});
};
What's driving me crazy is that sometimes it goes through on the first go, sometimes it takes 5 times. And the whole process is super slow. Here's the log I get when it doesn't come through:
XMLHttpRequest cannot load https://www.foo.bar/hangouts. No 'Access-Control-Allow-Origin' header
is present on the requested resource. Origin 'https://ts6d5n5om59gt6cin9c39faccjf890k5-a-hangout-
opensocial.googleusercontent.com' is therefore not allowed access.
I'm using Node + Express ~4 on Heroku.
I think the problem had something to do with pre-flight requests. I changed the AJAX call to the following:
$.ajax({
type: 'POST',
url: rootURL + "/hangouts",
dataType: "json",
data: {
"hangouts_id" : hangoutId,
"start_data" : startData
},
error: function( error ){
// Log any error.
console.log( "ERROR:", error );
// Try again
if (shouldpostHangoutId) {
postHangoutId(hangoutId); // Try again
};
},
complete: function(){
console.log( "postHangoutId -- success" ); // server response
shouldpostHangoutId = false;
}
});
And it goes right through, first time without delay.

ajax - GET request keeps using same old cookie on each session

Here is my two ajax requests:
$.ajax({
type: "POST",
contentType: "application/x-www-form-urlencoded ",
url: "https://example.com/j_spring_security_check",
data: { j_username: "mmm#mmmm.com", j_password: "mmmm" },
error: function (request, status, error) {
alert("POST LOG IN REQUEST:\t" + request + "\nSTATUS:\t" + status +
"\nERROR:\t" + error);
}
});
$.ajax({
type: "GET",
dataType: "json",
xhrFields: {
withCredentials: true
},
url: "https://mmm.com/rest/xxx/",
success: function(output, status, xhr) {
var flight = output;
alert(flight[0].flight.toAirport.name);
//return flight;
},
error: function (request, status, error) {
alert("GET ALL FLIGHTS REQUEST:\t" + request + "\nSTATUS:\t" + status +
"\nERROR:\t" + error);
}
});
When I run the code and use firebug - the second request keeps using the same old cookie in every new session. What I want to happen is that the second request will use the cookie from my first POST(login) request. I have tried several variations of withCredentials: true/false and crossDomain: true/false on both requests.
When i clear the cookies, the get request will create it's own cookie. Then I refresh the page to let the script run again. The POST request will create a new cookie(as it should), but the get request will re-use the cookie it made before.

Ajax not fetching data but Wget getting the same

I am quering a REST api hosted at amazon servers from my local system. I have shut out the cross domain origin restriction in Google chrome while firing the ajax.
Yet the ajax call fails.
I tried to wget the same URL and also tried file_get_contents() of PHP and received the required JSON object.
Is it possible that at the amazon servers, they have restricted all the ajax calls as in the Google Network tab i can see that the ajax call fails(status).
CODE::
$.ajax({
url :"URL to ec2",
type : 'get',
dataType: "jsonp",
contentType: 'application/json',
success: function (result) {
console.log("OOHKK ::" + result);
},
error : function (xhr, desc, err) {
console.log(xhr);
console.log("Desc: " + desc + "\nErr:" + err);
}
});

Resources