Request format is unrecognized for URL unexpectedly ending in - asp.net
When consuming a WebService, I got the following error:
Request format is unrecognized for URL unexpectedly ending in /myMethodName
How can this be solved?
Found a solution on this website
All you need is to add the following to your web.config
<configuration>
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
</configuration>
More info from Microsoft
Despite 90% of all the information I found (while trying to find a solution to this error) telling me to add the HttpGet and HttpPost to the configuration, that did not work for me... and didn't make sense to me anyway.
My application is running on lots of servers (30+) and I've never had to add this configuration for any of them. Either the version of the application running under .NET 2.0 or .NET 4.0.
The solution for me was to re-register ASP.NET against IIS.
I used the following command line to achieve this...
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -i
Make sure you're using right method: Post/Get, right content type and right parameters (data).
$.ajax({
type: "POST",
url: "/ajax.asmx/GetNews",
data: "{Lang:'tr'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) { generateNews(msg); }
})
Superb.
Case 2 - where the same issue can arrise) in my case the problem was due to the following line:
<webServices>
<protocols>
<remove name="Documentation"/>
</protocols>
</webServices>
It works well in server as calls are made directly to the webservice function - however will fail if you run the service directly from .Net in the debug environment and want to test running the function manually.
For the record I was getting this error when I moved an old app from one server to another. I added the <add name="HttpGet"/> <add name="HttpPost"/> elements to the web.config, which changed the error to:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at BitMeter2.DataBuffer.incrementCurrent(Int64 val)
at BitMeter2.DataBuffer.WindOn(Int64 count, Int64 amount)
at BitMeter2.DataHistory.windOnBuffer(DataBuffer buffer, Int64 totalAmount, Int32 increments)
at BitMeter2.DataHistory.NewData(Int64 downloadValue, Int64 uploadValue)
at BitMeter2.frmMain.tickProcessing(Boolean fromTimerEvent)
In order to fix this error I had to add the ScriptHandlerFactory lines to web.config:
<system.webServer>
<handlers>
<remove name="ScriptHandlerFactory" />
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</handlers>
</system.webServer>
Why it worked without these lines on one web server and not the other I don't know.
In my case the error happened when i move from my local PC Windows 10 to a dedicated server with Windows 2012.
The solution for was to add to the web.config the following lines
<webServices>
<protocols>
<add name="Documentation"/>
</protocols>
</webServices>
I use following line of code to fix this problem. Write the following code in web.config file
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000"/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>
I did not have the issue when developing in localhost. However, once I published to a web server, the webservice was returning an empty (blank) result and I was seeing the error in my logs.
I fixed it by setting my ajax contentType to :
"application/json; charset=utf-8"
and using :
JSON.stringify()
on the object I was posting.
var postData = {data: myData};
$.ajax({
type: "POST",
url: "../MyService.asmx/MyMethod",
data: JSON.stringify(postData),
contentType: "application/json; charset=utf-8",
success: function (data) {
console.log(data);
},
dataType: "json"
});
I also got this error with apache mod-mono. It looks like the documentation page for webservice is not implemented yet in linux. But the webservice is working despite this error. You should see it by adding ?WSDL at the end of url, i.e http://localhost/WebService1.asmx?WSDL
In html you have to enclose the call in a a form with a GET with something like
label
You can also use a POST with the action being the location of the web service and input the parameter via an input tag.
There are also SOAP and proxy classes.
In my case i had an overload of function that was causing this Exception, once i changed the name of my second function it ran ok, guess web server doesnot support function overloading
a WebMethod which requires a ContextKey,
[WebMethod]
public string[] GetValues(string prefixText, int count, string contextKey)
when this key is not set, got the exception.
Fixing it by assigning AutoCompleteExtender's key.
ac.ContextKey = "myKey";
In our case the problem was caused by the web service being called using the OPTIONS request method (instead of GET or POST).
We still don't know why the problem suddenly appeared. The web service had been running for 5 years perfectly well over both HTTP and HTTPS. We are the only ones that consume the web service and it is always using POST.
Recently we decided to make the site that host the web service SSL only. We added rewrite rules to the Web.config to convert anything HTTP into HTTPS, deployed, and immediately started getting, on top of the regular GET and POST requests, OPTIONS requests. The OPTIONS requests caused the error discussed on this post.
The rest of the application worked perfectly well. But we kept getting hundreds of error reports due to this problem.
There are several posts (e.g. this one) discussing how to handle the OPTIONS method. We went for handling the OPTIONS request directly in the Global.asax. This made the problem dissapear.
protected void Application_BeginRequest(object sender, EventArgs e)
{
var req = HttpContext.Current.Request;
var resp = HttpContext.Current.Response;
if (req.HttpMethod == "OPTIONS")
{
//These headers are handling the "pre-flight" OPTIONS call sent by the browser
resp.AddHeader("Access-Control-Allow-Methods", "GET, POST");
resp.AddHeader("Access-Control-Allow-Headers", "Origin, Content-Type, Accept, SOAPAction");
resp.AddHeader("Access-Control-Max-Age", "1728000");
resp.End();
}
}
I was getting this error until I added (as shown in the code below) $.holdReady(true) at the beginning of my web service call and $.holdReady(false) after it ends. This is jQuery thing to suspend the ready state of the page so any script within document.ready function would be waiting for this (among other possible but unknown to me things).
<span class="AjaxPlaceHolder"></span>
<script type="text/javascript">
$.holdReady(true);
function GetHTML(source, section){
var divToBeWorkedOn = ".AjaxPlaceHolder";
var webMethod = "../MyService.asmx/MyMethod";
var parameters = "{'source':'" + source + "','section':'" + section + "'}";
$.ajax({
type: "POST",
url: webMethod,
data: parameters,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
xhrFields: {
withCredentials: false
},
crossDomain: true,
success: function(data) {
$.holdReady(false);
var myData = data.d;
if (myData != null) {
$(divToBeWorkedOn).prepend(myData.html);
}
},
error: function(e){
$.holdReady(false);
$(divToBeWorkedOn).html("Unavailable");
}
});
}
GetHTML("external", "Staff Directory");
</script>
Make sure you disable custom errors. This can mask the original problem in your code:
change
<customErrors defaultRedirect="~/Error" mode="On">
to
<customErrors defaultRedirect="~/Error" mode="Off">
Related
How to call wcf service using ajax
I'm trying to call a WCF service from ajax call. This is my ajax call: $j.ajax( { type: "POST", contentType: "application/json; charset=utf-8", dataType: "json", context: this, async: false, url: appServicePath + "Student/GetRolesByUserId", data: JSON.stringify({ "userId": userId }), success: this.getRolesByIdResponse, error: this.getRolesByIdFailure } ); when I test my service using Fiddler I'm able to get the roles of the user that I'm passing its value from ajax but when I call the service from my application I get an error: 405 Method Not Allowed. What am I doing wrong?
According to your description, I think this problem is due to cross-domain, the problem generates an OPTIONS request, So the method is not allowed. If you put the script in the same domain as the webservice, you won't have this problem. Here is Http 405 method document. https://www.rfc-editor.org/rfc/rfc7231#section-6.5.5 You now need to turn on cross-domain support for a WCF application hosted on the web server side. Web.config <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*"/> <add name="Access-Control-Allow-Headers" value="content-type" /> <add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" /> </customHeaders> </httpProtocol> </system.webServer> Global.asax protected void Application_BeginRequest(object sender, EventArgs e) { if (Request.Headers.AllKeys.Contains("Origin")&&Request.HttpMethod=="OPTIONS") { Response.End(); } } Feel free to contact me if you have any questions.
Why does call to web service (.asmx) fail when deployed to Azure [duplicate]
When consuming a WebService, I got the following error: Request format is unrecognized for URL unexpectedly ending in /myMethodName How can this be solved?
Found a solution on this website All you need is to add the following to your web.config <configuration> <system.web> <webServices> <protocols> <add name="HttpGet"/> <add name="HttpPost"/> </protocols> </webServices> </system.web> </configuration> More info from Microsoft
Despite 90% of all the information I found (while trying to find a solution to this error) telling me to add the HttpGet and HttpPost to the configuration, that did not work for me... and didn't make sense to me anyway. My application is running on lots of servers (30+) and I've never had to add this configuration for any of them. Either the version of the application running under .NET 2.0 or .NET 4.0. The solution for me was to re-register ASP.NET against IIS. I used the following command line to achieve this... C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -i
Make sure you're using right method: Post/Get, right content type and right parameters (data). $.ajax({ type: "POST", url: "/ajax.asmx/GetNews", data: "{Lang:'tr'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { generateNews(msg); } })
Superb. Case 2 - where the same issue can arrise) in my case the problem was due to the following line: <webServices> <protocols> <remove name="Documentation"/> </protocols> </webServices> It works well in server as calls are made directly to the webservice function - however will fail if you run the service directly from .Net in the debug environment and want to test running the function manually.
For the record I was getting this error when I moved an old app from one server to another. I added the <add name="HttpGet"/> <add name="HttpPost"/> elements to the web.config, which changed the error to: System.IndexOutOfRangeException: Index was outside the bounds of the array. at BitMeter2.DataBuffer.incrementCurrent(Int64 val) at BitMeter2.DataBuffer.WindOn(Int64 count, Int64 amount) at BitMeter2.DataHistory.windOnBuffer(DataBuffer buffer, Int64 totalAmount, Int32 increments) at BitMeter2.DataHistory.NewData(Int64 downloadValue, Int64 uploadValue) at BitMeter2.frmMain.tickProcessing(Boolean fromTimerEvent) In order to fix this error I had to add the ScriptHandlerFactory lines to web.config: <system.webServer> <handlers> <remove name="ScriptHandlerFactory" /> <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </handlers> </system.webServer> Why it worked without these lines on one web server and not the other I don't know.
In my case the error happened when i move from my local PC Windows 10 to a dedicated server with Windows 2012. The solution for was to add to the web.config the following lines <webServices> <protocols> <add name="Documentation"/> </protocols> </webServices>
I use following line of code to fix this problem. Write the following code in web.config file <configuration> <system.web.extensions> <scripting> <webServices> <jsonSerialization maxJsonLength="50000000"/> </webServices> </scripting> </system.web.extensions> </configuration>
I did not have the issue when developing in localhost. However, once I published to a web server, the webservice was returning an empty (blank) result and I was seeing the error in my logs. I fixed it by setting my ajax contentType to : "application/json; charset=utf-8" and using : JSON.stringify() on the object I was posting. var postData = {data: myData}; $.ajax({ type: "POST", url: "../MyService.asmx/MyMethod", data: JSON.stringify(postData), contentType: "application/json; charset=utf-8", success: function (data) { console.log(data); }, dataType: "json" });
I also got this error with apache mod-mono. It looks like the documentation page for webservice is not implemented yet in linux. But the webservice is working despite this error. You should see it by adding ?WSDL at the end of url, i.e http://localhost/WebService1.asmx?WSDL
In html you have to enclose the call in a a form with a GET with something like label You can also use a POST with the action being the location of the web service and input the parameter via an input tag. There are also SOAP and proxy classes.
In my case i had an overload of function that was causing this Exception, once i changed the name of my second function it ran ok, guess web server doesnot support function overloading
a WebMethod which requires a ContextKey, [WebMethod] public string[] GetValues(string prefixText, int count, string contextKey) when this key is not set, got the exception. Fixing it by assigning AutoCompleteExtender's key. ac.ContextKey = "myKey";
In our case the problem was caused by the web service being called using the OPTIONS request method (instead of GET or POST). We still don't know why the problem suddenly appeared. The web service had been running for 5 years perfectly well over both HTTP and HTTPS. We are the only ones that consume the web service and it is always using POST. Recently we decided to make the site that host the web service SSL only. We added rewrite rules to the Web.config to convert anything HTTP into HTTPS, deployed, and immediately started getting, on top of the regular GET and POST requests, OPTIONS requests. The OPTIONS requests caused the error discussed on this post. The rest of the application worked perfectly well. But we kept getting hundreds of error reports due to this problem. There are several posts (e.g. this one) discussing how to handle the OPTIONS method. We went for handling the OPTIONS request directly in the Global.asax. This made the problem dissapear. protected void Application_BeginRequest(object sender, EventArgs e) { var req = HttpContext.Current.Request; var resp = HttpContext.Current.Response; if (req.HttpMethod == "OPTIONS") { //These headers are handling the "pre-flight" OPTIONS call sent by the browser resp.AddHeader("Access-Control-Allow-Methods", "GET, POST"); resp.AddHeader("Access-Control-Allow-Headers", "Origin, Content-Type, Accept, SOAPAction"); resp.AddHeader("Access-Control-Max-Age", "1728000"); resp.End(); } }
I was getting this error until I added (as shown in the code below) $.holdReady(true) at the beginning of my web service call and $.holdReady(false) after it ends. This is jQuery thing to suspend the ready state of the page so any script within document.ready function would be waiting for this (among other possible but unknown to me things). <span class="AjaxPlaceHolder"></span> <script type="text/javascript"> $.holdReady(true); function GetHTML(source, section){ var divToBeWorkedOn = ".AjaxPlaceHolder"; var webMethod = "../MyService.asmx/MyMethod"; var parameters = "{'source':'" + source + "','section':'" + section + "'}"; $.ajax({ type: "POST", url: webMethod, data: parameters, contentType: "application/json; charset=utf-8", dataType: "json", async: true, xhrFields: { withCredentials: false }, crossDomain: true, success: function(data) { $.holdReady(false); var myData = data.d; if (myData != null) { $(divToBeWorkedOn).prepend(myData.html); } }, error: function(e){ $.holdReady(false); $(divToBeWorkedOn).html("Unavailable"); } }); } GetHTML("external", "Staff Directory"); </script>
Make sure you disable custom errors. This can mask the original problem in your code: change <customErrors defaultRedirect="~/Error" mode="On"> to <customErrors defaultRedirect="~/Error" mode="Off">
Cors issues using sharepoint
i'm trying to query using REST API (sharepoint) and get some results. this is where i've started https://www.slideshare.net/CoreyRoth/fives-ways-to-query-share-point-2013-search-sharepoint-summit-toronto-2013, slide 31 this is what i have document.getElementById("restApi") .addEventListener("click", restApi, false); function restApi() { var queryUrl = "https://myAppDomain.org/_api/search/query?querytext='" + $("#restSearch").val() + "'"; $.ajax({ type: "GET", url: queryUrl, xhrFields: { withCredentials: true }, headers: { "Accept": "application/json; odata=verbose" }, success: function (data) { var results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results; }, error: function (error) { } }); } this is the problem. XMLHttpRequest cannot load "https://myAppDomain.org/_api/search/query?querytext=x" No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63734' is therefore not allowed access. and another way i've tried providing password and username headers: { "Accept": "application/json; odata=verbose", "password": "my password", "username": "my account name" }, for which i get XMLHttpRequest cannot load https://myAppDomain.org/_api/search/query?querytext=x. 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:63734' is therefore not allowed access. The response had HTTP status code 401. I'm using .net webApi, for which i've installed the cors packages, configuration // Enabled cors config.EnableCors(new EnableCorsAttribute("*", "*", "*") { SupportsCredentials = true }); The app are internal and i'm using windows credentials pass and account name. So why am i getting this error, what do i do wrong?, i've searched and tried other solutions but always get the same problem related to No 'Access-Control-Allow-Origin' header is present on the requested
Open IIS Go to Sites and find your Sharepoint site Right-click -> Explore Open web.config Add the following code inside system.webServer node <?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> </customHeaders> </httpProtocol> </system.webServer> </configuration> Save and exit
I've managed to solve my issue (partially, because i would have wanted to use CSOM rather then REST API on client side) by allowing cross domain calls from my application, directly to the application(sharepoint app) for which i made the request. So in the requested app i've added the following file : web.config in section : system.webserver/httpprotocol/customheaders content : <add name="Access-Control-Allow-Origin" value="https://myApplicationDomain.zzz" /> <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" /> <add name="Access-Control-Allow-Headers" value="Content-Type" /> <add name="Access-Control-Allow-Credentials" value="true" />
If the server is being developed by you Have you put the "Access-Control-Allow-Origin: localhost" in your .htaccess or in your webserver configuration? You can, also, add that header in your response from the server.
Different declaration of a WebMethod in asmx and aspx file?
I have the exact code when declaring webmethod in aspx file and in asmx file. They are webmethods exposed for client scripting. I just want to use webmethod inside asmx file, but cannot get it to work. When I reference a method in aspx file everything works just fine, but when I reference webmethod in asmx I receive an error method unknown. I checked all solutions for "unknown method, parametar methodname" but nothing helped. Webmethod is both declared in a similar way: [WebMethod] public static string[] InsertRecord(string param) { return something } Only difference is that asmx contains [System.Web.Script.Services.ScriptService] for class. I cant figure out what is the problem. WebMethod is being called from Jquery script places in a control (ascx). function InsertRecord(notice) { $.ajax({ type: "POST", url: "/Webservices/Records.asmx/InsertRecord", data: "{ 'notice':'" + notice + '' }", contentType: "application/json; charset=utf-8", dataType: "json", success: function(response) { }, error: function(msg) {} }); }
your web.config file maybe needs this (check if it is there): <webServices> <protocols> <add name="HttpSoap"/> <add name="HttpPost"/> <add name="HttpGet"/> <add name="Documentation"/> </protocols> </webServices> you neeed to uset httppost and httpget in your web.config file, or your ajax call will never happen.
VB.NET ASMX returning the response header as XML instead of JSON
My ASMX call is returning Content-Type = text/xml; charset=utf-8 I am using ASP.NET 3.5 and jQuery.Ajax. Have added these things are per numerous suggestions from Stack Overflow I have done these things Done this at web.config <remove verb="*" path="*.asmx"/> <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> On the function call inside ASMX, I am using these <WebMethod()> _ <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _ Public Function GetCompanies(SearchedCompany As String) As String the jQuery Ajax call is like this var searchString = "SearchedCompany=" + $(".auto-company").val(); $.ajax({ type: "POST", url: "Services/CompaniesService.asmx/GetCompanies", data: searchString, dataType: 'json', success: function (msg) { companies = msg.hasOwnProperty("d") ? msg.d : msg; }, error: function (xhr, status, error) { } }); But whatever I do, the response header is still of type xml. What part am I missing here? Disclaimer: I know this has been asked many times around here. But was not able to find any answer. As you can see, I have already implemented all of the solutions provided.
You need to set the contentType as well to "application/json; charset=utf-8" http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/ Because jQuery does set the content-type on POST requests that have data, the content-type gets set to the default “application/x-www-form-urlencoded” initially. Then in IE7, setRequestHeader was adding “application/json; charset=utf-8″ to that instead of truly setting it. So, the content-type was incorrect for JSON serialization and the web service was returning XML instead of JSON.