I ran into an issue where I have an Ajax Post that generates a document serverside. It works great on localhost but when it's published on server side, I receive a 404 in the console.
Here's my code ajax call:
$("#btn1330").on("click", function () {
$("#loading").show();
$.ajax({
method: "POST",
url: "/AdminReports/GenerateForm1330",
data: {
urd: {
"URSID": $(".form-1330-input").val() //this is a string value
}
}
}).done(function () {
$("#loading").hide();
});
});
It is received on controller side (AdminReports) with this:
[HttpPost]
public JsonResult GenerateForm1330(UnlimitedReleaseDocument urd)
{
//my code for generating a document and saving to desktop is here
return Json(true)
}
On localhost, this works as expected with a 200 success, however, when pushed to staging, I see the following error in console:
> POST https://urs-staging.jpl.nasa.gov/AdminReports/GenerateForm1330
> 404 (Not Found)
Any ideas as to why this doesn't work on staging/production? The URL in the console error is exactly the route that it needs to hit so I'm confused! Thanks in advance!
Related
I am trying to integrate atoms payment gateway in my MVC project. I have been getting this nasty error even after enabling CORS. So I thought perhaps it is the return url that is causing the issue. So I wrote a simple redirect to find out real cause return Redirect("https://google.com"); but i am still getting same error. What am I doing wrong?
public ActionResult TransferFund()
{
return Redirect("https://google.com");
}
Failed to load https://google.com/: Response to preflight request doesn't pass
access control check: No 'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://localhost:25850' is therefore not allowed
access. The response had HTTP status code 405.
I have followed This to redirect
EDIT
<script type="text/javascript">
$(document).ready(function () {
$('#btnatom').on('click', function () {
$.ajax({
type: 'POST',
url: "/AtomPayment/TransferFund",
data: {},
dataType: "text",
success: function (resultData) { alert("Save Complete") }
});
});
});
</script>
[HttpPost]
public ActionResult TransferFund()
{
return new RedirectResult("http://google.com");
string strURL, strClientCode, strClientCodeEncoded;
....
}
You could not make redirect in 'TransferFund' method, becuase of 'POST' method. Do it from jquery. Here corrected code:
<script type="text/javascript">
$(document).ready(function () {
$('#btnatom').on('click', function () {
$.ajax({
type: 'POST',
url: "/AtomPayment/TransferFund",
data: {},
dataType: "text",
success: function (resultData) {
if(resultData == "Ok"){
alert("Save Complete");
window.location = "https://google.com/"
}
}
});
});
});
</script>
[HttpPost]
public ActionResult TransferFund()
{
//do some transfer work
return Content("Ok");
}
let's drill down into the error:
Failed to load https://google.com/: Response to preflight request
doesn't pass access control check: No 'Access-Control-Allow-Origin'
header is present on the requested resource. Origin
'http://localhost:25850' is therefore not allowed access. The
response had HTTP status code 405.
you are hitting a Cross Domain request issue.
Google is denying you, for security reasons from reaching it's server. It doesn't matter if you issue POST or GET request, google doesn't recognize your domain and blocks your cross-domain request.
If you use jquery it will work, yes, because you make a request from client-to-client using the window.location property of java script.
if you want to RedirectResult from your controller code, you need to use address at your local domain that will accept the request and won't deny it.
you can read more here about Error 405.
what it means, is:
The administrator can configure each web server so that the individual
methods are either allowed or not allowed. For example, if there’s no
interactive content on the website, it’s only logical that the POST
method isn’t allowed, since the user has no options to enter their own
data and send it to the server. Otherwise, the error message mentioned
above with the status code 405 would appear, informing the browser and
its user that the method is not allowed.
Just Incase anyone needs:
First redirect your other "http://example.com" to another controller ("RedirectController") without httpMethod
[HttpPost]
public IActionResult TransferFund(){
Return Redirect("redirect?url=http://example.com");
}
Now in:
RedirectController.cs
RedirectController(){
public IActionResult Indext(){
var url = Request.Query["url"];
Return Redirect(url);
}
}
or we can redirect to current controller btw
I have a small angularjs application where I'm trying to call a server side function.
I'm not sure but I feel kind a lost when it comes to use the right url for GET/POST from client to server...But I'm not sure the problem is there..
My angular:
$http.get({
method: 'GET',
url: '/Models/Person/GetTestPersons'
}).then(function successCallback(response) {
alert(response);
}, function errorCallback(response) {
alert("ErrorCallback");
});
My server function (Models/Person.cs):
public static string[] GetTestPersons()
{
return new[]
{
"Person1",
"Person2"
};
}
Now I get to the "alert("ErrorCallback")"
And if I could get this to work.. How do I read the array that is returned?
Error:
GET http://localhost:51203/Views/[object%20Object] 404 (Not Found)
You ought to create a Web API project,
The controller will orchestrate call to your Person model.
A quick starter tutorial is here
http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
I am working on vNext, and trying to create a web api in asp.net vNext. following are the sample code that i used to create api
public class UserController : Controller
{
public IActionResult Get()
{
return Result.Json(new { message = "Valid" });
}
}
I can access this api on browser directly and even i can access this api in same domain view by ajax get request, sample code is below
$.ajax({
type: "GET",
// Get the action URL
url: "http://mydomainname/api/user",
dataType: "json",
success: function (data, textStatus, jqXHR) {
alert(data.message);
},
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
But, when i am trying to access this api by means any other domain or jfiddler , it is not working.
check this jfiddler link
http://jsfiddle.net/2r8wj62a/14/
I am using VS2014 CPT.
an i missing anything... any help will appreciated.
Thanks in Advance...!
beta.smallshiptravel.com resolves to ip 74.114.167.68 on my end. Is it the same on your end?
I've tried the link and it is returning:
HTTP/1.1 404 Not Found
Content-Type: text/html
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
So it looks like you are running inside iis and iis returns not found. I would check the iis logs if the request are actually ending up inside your iis server, or if another server (or reverse proxy) is returning the calls. It it does end up in your server then I would add some logging in my code to every request so i could see it my code actually gets called.
HTH,
Bart
I'm doing a project for college where one WebSite sends commands to a Windows Forms application. This application is responsible for access to serial port and send and receive commands.
Communication between the Website and the Windows Forms application, I used Web Api, however after publishing, auditioning remembered that the localhost in a C # WebSite. Net's own site and not my Windows Forms application.
I changed the call to the Web Api directly use Ajax and not a controller.
In the examples I found I saw that I use JSONP, but I can not read the results and use it on my website.
The calling code and return seen by Chrome are below
function cmdLocal() {
$.ajax({
type: "GET",
dataType: "jsonp",
url: "http://local host:8089/api/gps/",
jsonpCallback: "cmdTorre",
jsonp: "cmdTorre"
});
}
function cmdTorre(data) {
alert(data);
}
Response Header
Content-Length:10
Content-Type:application/json; charset=utf-8
Date:Tue, 10 Jun 2014 11:18:30 GMT
Server:Microsoft-HTTPAPI/2.0
Response
No Properties
Windows Forms APIController
namespace TCCWindows.Lib
{
public class GPSController : ApiController
{
[HttpGet]
public string Posicao()
{
var coordenada = TCCWindows.FormPrincipal.PegarCoordenadas();
return coordenada.Latitude + "|" + coordenada.Longitude + "|" + coordenada.Altitude;
}
}
}
First, you ajax call looks overly complicated try replacing it with :
function cmdLocal() {
$.ajax({
type: "GET",
dataType: "jsonp",
url: "http://local host:8089/api/gps/",
success: cmdTorre,
error: function(err){
alert("You have a error"+err);
}
});
}
function cmdTorre(data) {
alert(data);
}
Please validate the new code carefully. I just typed it in here so can have errors. If this runs at this point you should probably see the error message. That is because your GPSController doesnt seem to be returning a valid JSONP (or JSON for that matter). Please read up on JSONP for more clarification but, I think if you modify your return statement to make it look like following, it should work. Assuming your controller is actually getting called and your network stuff is working:
return "cmdTorre({\"lat\":"+coordenada.Latitude+" , \"lon\":"+coordenada.Longitude+" });"
Basically your return string should look like following when printed on console:
function cmdTorre({
"lat": 23.34,
"lon":34.23,
"alt":50
});
Again I suggest you check the code I wrote for syntax issues as i just typed it up in here, but it should give you the idea.
So problems were:
The return string you are generating is NOT in JSON format
It is also not wrapped in a function call making it a invalid JSONP too.
Lastly my solution should get your code working and JSONP started but its not the right way to do things. Its more of a ugly hack. Your GPS controller should read the HTTP request for parameter called 'callback' which is a accepted convention for JSONP calls. Then instead of hardcoding the function name in the return statement, you should use the value of this callback parameter. Then you dont need to use a specific function like 'cmdTorre' in your jQuery. Instead a anonymus function like success:function(response){...} will work just fine.
Hope that helps.
Im trying to call a server method in my aspx.cs to delete all the files in a directory when a user closes the browser.
[WebMethod]
public static void fileDelete()
{
string[] uploadedFiles = Directory.GetFiles(#"C:\Users\Lambo\Documents\Visual Studio 2010\Projects\test\test\testPdfIn");
foreach (string uploaded in uploadedFiles)
{
File.Delete(uploaded);
}
}
======================================================================
EDIT
I've tried the POST method but it still doesn't seem to work. I've changed the URL too.
Over at the client side im using this:
$(function () {
$(window).unload(function () {
alert("Files have been deleted")
jQuery.ajax({ type: 'POST', url: "http://localhost:19642/success.aspx/fileDelete", async: true });
});
});
However it doesnt seem to be working. Are the codes wrong in someway?
To investigate failures of AJAX calls use HTTP debugger (like Fiddler ) to see what requests are made and what responses are received.
My guess is that your Url is wrong and request is made to wrong file. Consider making absolute (or at lest server relative) url.