I have a JQuery Mobile app. This app is currently running on localhost. I am relying on some services hosted on some of my backend servers. When I run a query, I noticed in fiddler that I am getting a 302. This 302 is causing my service call to fail.
If I copy-and-paste the URL of the service in my browser window, everything works fine. Here is my JQuery code
$.ajax({
url: GetServiceUrl(),
type: "GET",
data: vm,
contentType: "application/json",
success: service_Succeeded,
error: service_Failed
});
function service_Succeeded(result) {
alert("received!");
}
function service_Failed() {
alert("oops");
}
function GetServiceUrl() {
var url = "http://www.mydomain.com/services/endpoint";
return url;
}
Why am I getting a 302 from my app?
Thank you
your service is sending a redirect request. Maybe you require a login?
Related
I am rather new to Ionic App development and need help.
I am using Visual Studio 2015 for development and Chrome Browser with Ripple for rendering the application.
The problem is, when making an Ajax Post request to an API it gives me a 404 (Not Found error) because the POST url gets prepended with a rippleApi and TinyHippos Uri.
I am wondering what causes this prepending of the URI
The following is my Controller:
var request = $http({
method: "post",
url: myPostLink,
data: {},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
request.success(function (data) {
console.log(data);
});
I'm trying to make a simple Ajax call in my app on Openshift. This is my ajax call which is triggered by pressing a button:
$.ajax({
url: 'http://my-site-name.rhcloud.com/asciimo',
method: 'POST',
data: {attr:"value"}
});
And this is node in my server.js file:
self.createRoutes = function() {
self.routes['/asciimo'] = function(req, res) {
res.send('done');
};
};
Everything works when I go to my-site-name.rhcloud.com/asciimo, but if I click a button (to get there) I get:
POST http://my-site-name.rhcloud.com/asciimo 404 (Not Found)
even though the link clearly works on it's own.
Change your method to GET
method: 'GET',
When you go to the URL directly in your browser, you are issuing a GET request, not a POST.
I know what the problem was now. In ajax there is a POST request and self.routes['/asciimo'] (given as a template by Openshift) deals with GET requests only. What solved the problem was simply rewriting my function as a separate POST function:
self.addpost = function() {
self.app.post('/asciimo', function(req, res){
res.send('done');
});
};
I want to retrieve some data using $.ajax from the external ASP.NET MVC site (in this case - from my site). The code below geive me a 404 Not Found error (of course the url is valid.
But, if I change the url from url: 'http://myurl.com/Home/GetMyCode/?id=mycode' to url: 'http://localhost:123/Home/GetMyCode/?id=mycode' everything is fine. So, how to fix it ?
$.ajax({
url: 'http://myurl.com/Home/GetMyCode/?id=mycode',
type: 'POST',
contentType: "application/json; charset=utf-8",
crossDomain: true,
success: function (res) {
...
},
error: function (jqXHR, textStatus, errorThrown) {
...
}
});
[HttpPost]
public JsonResult GetMyCode(string id)
{
try
{
return Json(new { result = "ok", resultData = "OK") });
}
catch (Exception e)
{
return Json(new { result = "error", resultData = "An error occured" });
}
}
Two Methods for Handling Cross-Domain Ajax Calls:
JSONP: The Current Standard for Cross-Domain Access
JSONP is a convention used by some sites to expose their content in a way that makes it easier for callers to consume data via script, even from an external domain. The trick consists in having the site return some JSON content not as a plain string but wrapped up in a script function call. For more details..
http://www.west-wind.com/weblog/posts/2007/Jul/04/JSONP-for-crosssite-Callbacks
http://www.jquery4u.com/json/jsonp-examples/
Cross-origin resource sharing (CORS)
To enable cross-domain requests in environments that do not support cors yet but do allow cross-domain XHR requests (windows gadget, etc), set $.support.cors = true;
You just tell jQuery that you're in an environment where Cross-Domain XHR requests are possible.
In order to retrieve data crossdomain, you probably need to use 'jsonp'
Looks like it might be a DNS issue. Are you able to get to: http://myurl.com ?
Is the .com domain you are trying to access publicly accessible? Or is it a loopback to localhost?
that tutorial worked for me, I had to implement the JSONP handling in my MVC project. http://www.codeguru.com/csharp/.net/net_asp/using-jsonp-in-asp.net-mvc.htm
function girisAjaxKontrol() {
var kullanici = { 'kullaniciAdi': $('#username').val(), 'hidden': $('#password').val() };
$.ajax({
url: '/Giris/GirisGecerliMi',
type: 'POST',
data: kullanici,
success: girisAjaxReturn,
error: function (error, textstatus) {
JSON.stringify(error);
errorMessage($("div.girisSubmit input"), JSON.stringify(error), false);
}
});
}
This function gets the error below(it was working before deployment). The website is an Asp .Net MVC4 website on local IIS 7.5; I searched a lot but couldn't solve yet.
Server Error in Application \"DEFAULT WEB SITE\"
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
Module: IIS Web Core
Notification: MapRequestHandler
Handler: StaticFile
Error Code: 0x80070002
Requested URL http://localhost:80/Giris/GirisGecerliMi
Physical Path C:\\inetpub\\wwwroot\\Giris\\GirisGecerliMi
Logon Method Anonymous
Logon User Anonymous
url: '/Giris/GirisGecerliMi',
should not be hardcoded like this. You should use url helpers to generate this url. The reason for that is because when you deploy your application in IIS there's a virtual directory name that you should take into account, so the correct url is:
url: '/MyAppName/Giris/GirisGecerliMi',
But by hardcoding the url as you did there's no way for this to work. In ASP.NET MVC you should always use url helpers such as Url.Action to generate the url to a given controller action as those helpers take into account not only your routing definitions but things like virtual directory names as well.
So the correct way is to have this url generated server side and passed as argument to the girisAjaxKontrol function:
function girisAjaxKontrol(url) {
var kullanici = { 'kullaniciAdi': $('#username').val(), 'hidden': $('#password').val() };
$.ajax({
url: url,
type: 'POST',
data: kullanici,
success: girisAjaxReturn,
error: function (error, textstatus) {
JSON.stringify(error);
errorMessage($("div.girisSubmit input"), JSON.stringify(error), false);
}
});
}
I am trying to call via ajax a WebMethod hosted in a traditional ASP.Net WebForm code-behind page. Here is the code for the WebMethod:
[WebMethod]
public static object States()
{
StateProvince[] states = new StateProvince[] { };
ApplicationServiceClient proxy = null;
try
{
proxy = new ApplicationServiceClient();
states = proxy.GetStateProvinces();
}
finally
{
CloseServiceProxy(proxy);
}
return states;
}
The WebMethod works just fine in my stand-alone development environment or if deployed normally to IIS, but when I deploy the aspx page to Sitefinity, I get the following exception when it's called:
Server Error in '/' Application.
Unknown web method States.aspx.
Parameter name: methodName
I'm not sure if Sitefinity is hijacking the URL and post-pending the ".aspx" or what is going on here.
Here is the jquery call that is calling the web method:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Regions.aspx/States",
data: "{}",
success: function(data) {
// implementation omitted
},
error: function(xhr, msg) {
alert(xhr.responseText);
}
});
I can replicate the problem by posting the request manually using Fiddler.
Again, this works just fine everywhere except when deployed to Sitefinity. Anybody have any thoughts?
What version of .NET is Sitefinity running? Page methods are a recent addition.
use dataType: "json",