Ignore case Sensitiveness of Jquery AJAX call URL - c#

Currently, I am working on Asp.net 3.5 project and I am calling a Webservice by ajax call
$.ajax({
type: "Post",
contentType: "application/json; charset=utf-8",
url: "WebService/DefaultPage.asmx/GetTours",
dataType: "json",
success: function (data) {
Home.ReadTours(data);
},
error: function (result) {
$('.load-3').hide();
}
});
working fine.
but when I change the URL to lowercase.
url: "webservice/defaultpage.asmx/gettours",
its not working.
Is this Jquery Ajax URL is case sensitive and is there any by I can ignore case sensitiveness of ajax URL.

Your server fielding the ajax request will need to be configured to NOT be case sensitive. There is nothing you can do in the client to change case sensitivity of a server (other than providing the case that the server expects).

Related

ASP.NET MVC3 project, ajax callback works with " contentType: 'text/json; charset=utf-8' " but " contentType: 'application/json; charset=utf-8' "

Recently, I convert my company's mvc2 site to mvc3. Most features went well but ajax call failed. the js code locates on aspx page(it was mvc 2)
Original code:
$.ajax({ type: 'POST',
url: '<%= Url.Action("MethodName", "Home") %>',
data: JSON.stringify(results), //results is some data with json format
traditional: true,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: otherJSFunction
})
When I ran the code, I got an error from C# as following.
" System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName) "
I tried to add attribute [DataContract] to Model class but no use. When I tried to change the original code's "application" to "text", it works. Code as following:
$.ajax({ type: 'POST',
url: '<%= Url.Action("MethodName", "Home") %>',
data: JSON.stringify(results), ////results is some data with json format
traditional: true,
dataType: 'json',
contentType: 'text/json; charset=utf-8',
success: otherJSFunction
})
I checked a lot of tickets from stackoverflow and documents, people always say "application/json" is the right way. But the right way doesn't work on my case. Is there anyone can help me to explain why this happened?
Thank you.

.Net Core ValidateAntiForgeryToken throwing web api 400 error

Visual Studio 2017 with Web Api using .net Core 1.1 I'm using, but I am getting a 400 Bad Request Error.
Error Occurs in every way:
Angular http
Fiddler
Postman
SoapUI
Swagger
ASP.NET Web API “400 Bad Request” on POST Request
[HttpPut]
//[ValidateAntiForgeryToken]
public IActionResult Put([FromBody]VeteranInteraction sessionTracker)
{ //.... }
Why is this happening ?
ValidateAntiForgeryToken is the problem , if I comment it out it works.
You must send the AntiForgery token on every request
var arr = { City: 'Tehran', Age: 25 };
$.ajax({
url: "#Url.Action("SaveAccess", "Access")",
type: 'POST',
data: JSON.stringify(arr),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
beforeSend: function (request) {
request.setRequestHeader("RequestVerificationToken", $("[name='__RequestVerificationToken']").val());
},
success: function (msg) {
alert(msg);
}
});
You need to send the AntiForgery token on every request where it's being validated either as a cookie or as a http header.
Refer to the documentation here. They have a section on how to configure angular js to do it.

processing ajax post on the server using c#

I'm trying to do something that's very new to me. I've done some digging but what I've come up with only had solutions by using some kind of .aspx page to process forms.
What I'd like to do is send some form input via ajax (I'm using jquery) to the server to be processed by c#.
This is, I believe, a fairly general question. It doesn't have to do with submitting form input specifically, but more about how to send some data to be processed by c# in the backend.
I want to make an ajax POST to a certain page for processing such as this:
$.ajax({
type: "POST",
url: "****???****",
data: { name: "Bob", gender: "Male" },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var names = response.d;
alert(names);
}
});
Now, my question is, what kind of file/page can I use for the url value in the above ajax call, that will be able to handle the POSTed data using c#?
All the examples I've seen use a .aspx page that processes the form and sends it to a .cs code-behind page. If possible, I'd like to be able to use something other than an .aspx page.
The site I'm working on is using just HTML as I'm working with angular. Is there any other way I can handle ajax on the server in a way that I can use c#?
To be very specific, I'm trying to setup a registration page where a user puts in their username and password. I need the password to be sent to the server to have a hash and salt created in C#.
Thanks, and please let me know if you need any additional information to continue.
Try this
In code behind file:Default.aspx.cs
[WebMethod]
public static void GenerateHash(string name,string password)
{
//Write logic to generate hash
}
.aspx page
$.ajax({
type: "POST",
url: "Default.aspx/GenerateHash",
data: { name: "xxx", password: "xxxx" },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var names = response.d;
alert(names);
}
});

How to resolve uncaught syntax error in jsonp

I am accessing a cross domain api using jquery ajax but I can't achieve it . Iam getting error as "uncaught syntaxerror : unexpected token <" in the console, How to fix this.
Code:
$.ajax({
url: "http://..............",
type:"GET",
dataType: 'jsonp',
crossDomain: true,
contentType:"application/javascript",
success: function (data) {
alert(data);
},
error: function (errorMEssage, Errtext) {
alert(Errtext);
}
});
Error:
uncaught syntaxerror : unexpected token <
You cannot make cross domain AJAX calls using JSONP to a server that returns XML. If you want to be able to make a cross domain AJAX call you have 2 possibilities:
use JSONP -> your server needs to support it.
use CORS -> your server AND client browser need t support it.
If your server supports CORS your request may look like this:
$.ajax({
url: "http://..............",
type:"GET",
crossDomain: true,
success: function (data) {
alert(data);
},
error: function (errorMEssage, Errtext) {
alert(Errtext);
}
});
change the dataType to xml
dataType: 'xml',

C# in javascript function?

How can I call a function C# (server side) function in a Javascript function (client side) which triggers the browser is closed? as:
window.onbeforeclose = MyFunction() // C#
You can not call your server side function from client side , You will need to decorate your C# method with Web Method attribute and make it static to expose it as a callable AJAX member:
something like
[WebMethod]
public static void someCSharpFunction() {
// body of function
}
and in your client side
$.ajax({
type: "POST",
url: "SomePage.aspx/someCSharpFunction",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Do something interesting here.
}
});
Hope that helps.
If you are looking to call a server side (c#) method from JavaScript, there are few ways to interact with server side through client side(Javascript):
XMLHttpRequest
Callback
WebService call
PageMethod

Categories

Resources