Api post call works on postman but in mobile app or ajax call it doesn't
-
here's the problem I've got an API with the following address (HTTP post)
http://api.novin.solutions/api/member/login
"headers": {
"accept": "application/json",
"content-type": "application/x-www-form-urlencoded"
},"data": { "username": "mrkeivan", "password": "09126101185", "uiId": "d960a994-0972-44ba-ae2c-2c3a01e135c1" }
I need to call this api in another website and a mobile application, to do so I enabled Asp.net APi Cores globally in web config and WebApiConfig.cs **in WebApiConfig.cs** config.EnableCors(new EnableCorsAttribute("*", "*", "*")); > as well as Web.config
when I call this in postman, it works perfectly however when I call it through an ajax method or a friend of mine calls it in his mobile app project it returns the following error > XMLHttpRequest cannot load > http://api.novin.solutions/api/member/login. Response for preflight > has invalid HTTP status code 404 OPTIONS > http://api.novin.solutions/api/member/login 404 (Not Found) I have read many articles in this matter and applied all the suggestions, but nothing fixed my problem :( any help would be appreciated
-
here's the problem I've got an API with the following address (HTTP post)
http://api.novin.solutions/api/member/login
"headers": {
"accept": "application/json",
"content-type": "application/x-www-form-urlencoded"
},"data": { "username": "mrkeivan", "password": "09126101185", "uiId": "d960a994-0972-44ba-ae2c-2c3a01e135c1" }
I need to call this api in another website and a mobile application, to do so I enabled Asp.net APi Cores globally in web config and WebApiConfig.cs **in WebApiConfig.cs** config.EnableCors(new EnableCorsAttribute("*", "*", "*")); > as well as Web.config
when I call this in postman, it works perfectly however when I call it through an ajax method or a friend of mine calls it in his mobile app project it returns the following error > XMLHttpRequest cannot load > http://api.novin.solutions/api/member/login. Response for preflight > has invalid HTTP status code 404 OPTIONS > http://api.novin.solutions/api/member/login 404 (Not Found) I have read many articles in this matter and applied all the suggestions, but nothing fixed my problem :( any help would be appreciated
When I made a call to that api this is the response I got
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Vary: Accept-Encoding
Server: Microsoft-IIS/8.5
Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, PUT, DELETE, GET, OPTIONS
Access-Control-Allow-Headers: content-Type, accept, origin, X-Requested-With, Authorization, name
Access-Control-Allow-Credentials: true
Date: Thu, 09 Mar 2017 10:02:31 GMT
Content-Length: 135Note that there are multiple "Access-Control-Allow-Origin" headers. The first thing I would do is look to why that is happening and get it down to one as the browser may well reject the response as invalid (if I use IE I get an error in the console explicitly stating there are duplicate headers). This is probably happening as you are configuring it in multiple places.
-
When I made a call to that api this is the response I got
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Vary: Accept-Encoding
Server: Microsoft-IIS/8.5
Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, PUT, DELETE, GET, OPTIONS
Access-Control-Allow-Headers: content-Type, accept, origin, X-Requested-With, Authorization, name
Access-Control-Allow-Credentials: true
Date: Thu, 09 Mar 2017 10:02:31 GMT
Content-Length: 135Note that there are multiple "Access-Control-Allow-Origin" headers. The first thing I would do is look to why that is happening and get it down to one as the browser may well reject the response as invalid (if I use IE I get an error in the console explicitly stating there are duplicate headers). This is probably happening as you are configuring it in multiple places.
-
Hi, thanx for the response removed them, now I only have it in - web.config and - config.EnableCors(); in WebApiConfig
Now there are two
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Vary: Accept-Encoding
Server: Microsoft-IIS/8.5
Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
Date: Thu, 09 Mar 2017 11:15:52 GMT
Content-Length: 135You only need it defined once, but you are defining it in two config files
-
here's the problem I've got an API with the following address (HTTP post)
http://api.novin.solutions/api/member/login
"headers": {
"accept": "application/json",
"content-type": "application/x-www-form-urlencoded"
},"data": { "username": "mrkeivan", "password": "09126101185", "uiId": "d960a994-0972-44ba-ae2c-2c3a01e135c1" }
I need to call this api in another website and a mobile application, to do so I enabled Asp.net APi Cores globally in web config and WebApiConfig.cs **in WebApiConfig.cs** config.EnableCors(new EnableCorsAttribute("*", "*", "*")); > as well as Web.config
when I call this in postman, it works perfectly however when I call it through an ajax method or a friend of mine calls it in his mobile app project it returns the following error > XMLHttpRequest cannot load > http://api.novin.solutions/api/member/login. Response for preflight > has invalid HTTP status code 404 OPTIONS > http://api.novin.solutions/api/member/login 404 (Not Found) I have read many articles in this matter and applied all the suggestions, but nothing fixed my problem :( any help would be appreciated
If that's your real password that you've just posted on a public forum, I hope it doesn't provide access to any sensitive data, and that you're going to change it as soon as the problem is resolved. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Now there are two
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Vary: Accept-Encoding
Server: Microsoft-IIS/8.5
Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
Date: Thu, 09 Mar 2017 11:15:52 GMT
Content-Length: 135You only need it defined once, but you are defining it in two config files
-
If that's your real password that you've just posted on a public forum, I hope it doesn't provide access to any sensitive data, and that you're going to change it as soon as the problem is resolved. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Now there are two
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Vary: Accept-Encoding
Server: Microsoft-IIS/8.5
Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
Date: Thu, 09 Mar 2017 11:15:52 GMT
Content-Length: 135You only need it defined once, but you are defining it in two config files
this is what I get Request Accept:*/* Accept-Encoding:gzip, deflate, sdch Accept-Language:en-US,en;q=0.8 Access-Control-Request-Headers:cache-control Access-Control-Request-Method:POST Cache-Control:no-cache Connection:keep-alive Host:api.novin.solutions Origin:http://localhost:1344 Pragma:no-cache Referer:http://localhost:1344/Home/Index User-Agent:Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 Response Access-Control-Allow-Credentials:true Access-Control-Allow-Headers:content-Type, accept, origin, X-Requested-With, Authorization, name Access-Control-Allow-Methods:POST, PUT, DELETE, GET, OPTIONS Access-Control-Allow-Origin:* Connection:close Content-Length:1245 Content-Type:text/html Date:Fri, 10 Mar 2017 05:26:20 GMT Server:Microsoft-IIS/8.5 Vary:Accept-Encoding X-Powered-By:ASP.NET X-Powered-By-Plesk:PleskWin
-
Via ajax and I use Fiddler to examine the response
$(document).ready(function () {
$.ajax({
type: "POST",
url: "http://api.novin.solutions/api/member/login",
data: {
"username": "mrkeivan",
"password": "09126101185",
"uiId": "d960a994-0972-44ba-ae2c-2c3a01e135c1"
}
}).done(function (data) {
alert(data);
}).fail(function (jqXHR, textStatus) {
alert("Request failed: " + textStatus);
});
}); -
Via ajax and I use Fiddler to examine the response
$(document).ready(function () {
$.ajax({
type: "POST",
url: "http://api.novin.solutions/api/member/login",
data: {
"username": "mrkeivan",
"password": "09126101185",
"uiId": "d960a994-0972-44ba-ae2c-2c3a01e135c1"
}
}).done(function (data) {
alert(data);
}).fail(function (jqXHR, textStatus) {
alert("Request failed: " + textStatus);
});
}); -
here's the problem I've got an API with the following address (HTTP post)
http://api.novin.solutions/api/member/login
"headers": {
"accept": "application/json",
"content-type": "application/x-www-form-urlencoded"
},"data": { "username": "mrkeivan", "password": "09126101185", "uiId": "d960a994-0972-44ba-ae2c-2c3a01e135c1" }
I need to call this api in another website and a mobile application, to do so I enabled Asp.net APi Cores globally in web config and WebApiConfig.cs **in WebApiConfig.cs** config.EnableCors(new EnableCorsAttribute("*", "*", "*")); > as well as Web.config
when I call this in postman, it works perfectly however when I call it through an ajax method or a friend of mine calls it in his mobile app project it returns the following error > XMLHttpRequest cannot load > http://api.novin.solutions/api/member/login. Response for preflight > has invalid HTTP status code 404 OPTIONS > http://api.novin.solutions/api/member/login 404 (Not Found) I have read many articles in this matter and applied all the suggestions, but nothing fixed my problem :( any help would be appreciated
If that's your real password that you've just posted on a public forum, I hope it doesn't provide access to any sensitive data, and that you're going to change it as soon as the problem is resolved. Smile | :)
Best Web development Company.