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);
}
});
Related
I'm completely new to how soap services work, please correct me if my understanding is wrong. I would like to pass parameters and call a function from a soap service by typing in a url on my browser (Chrome) and then would like to see the results. I tried searching and following the information from here, but I'm not sure what I'm doing wrong. I have tried the following variations:
http://<servername>/apppath/MyService.asmx?op=GetData?loc=01&status=OPEN
http://<servername>/apppath/MyService.asmx/GetData?loc=01&status=OPEN
This is what I get when I go to the url.
http:/<servername>/apppath/MyService.asmx?op=GetData?
Please help.
Maybe you are requesting wrong urls? If you have .asmx in your application - you should be able to see the description page on the url
http://{servername}/{apppath}/MyService.asmx
Of course you should replace {servername} the {apppath} with your values.
You have to send an HTTP POST request in order to call your web service GetData.
Your JS code should be something like:
//url should be MyService.asmx/GetData
function callWS(url) {
var loc = "01";
var status = "OPEN";
var options = { error: function(msg) { alert(msg.d); },
type: "POST", url: "webmethods.aspx/UpdatePage",
data: JSON.stringify({ loc: loc, status: status }),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function(response) { alert(response); }
};
$.ajax(options);
}
So the error was my understanding of SOAP and host to use Postman. In short, I wasn't able to accomplish a SOAP request through the browser. Also, the picture supplied, it showed I was missing 2 things. 1) The SoapAction 2) The parameters were not supplied in the url but rather in the <soap:Body> tag. These were supplied in the POST and I was able to view my results in Postman
Basically I have a C# web service method that helps to generate HTML code and return it as a string. Now I would like to grab the HTML string from this method and replace a particular div.
function replaceHTML(ID) {
var gID = ID;
$.ajax({
type: "get",
contentType: //what should it be,
url: "the method location",
data: {"ID" : gID },
dataType: //what should it be,
success: function (data) {
$('#Div ID').empty();
$('#Div ID').html(data);
}
});
}
What should be the content Type and data Type? Am I doing it correctly?
Well it all depends on what your web service is providing you and since we cannot see the web service call, we cannot tell you.
In every average web service it should be specified what type of data it communicates with, whether json, xml, or plain text.
Usually though, it will be xml unless something else is specified because web services are SOAP based and are xml formatted, but you should check your web service or share it's code with us.
contentType is the type of data you send to the server, so it'll be application/json in your case. Not nessecary to provide that info though, jQuery will detect it based on the contents of data.
dataType is the type you expect the server to return, so you set it to text/html.
Also, see:
http://api.jquery.com/jquery.ajax/
Hope this will work for you :).
function replaceHTML(ID) {
var gID = ID;
$.ajax({
type: "get",
contentType: 'text',//what should it be,
url: "the method location",
data: JSON.stringify({"ID" : gID }),
dataType: "application/json; charset=utf-8",,
success: function (data) {
$('#Div ID').empty();
$('#Div ID').html(data);
}
}
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).
I'd like to ask if somebody has the idea on how to auto feed in the data to your webpage. What I mean is, if I'm uploading data to my website I want to automatically display it to the other page which is displayed in a separate tab because I have a photo gallery page and I want to see my uploaded files in that page without hitting the refresh button of the browser. All I've got right now is the auto-refresh codes but that doesn't apply it to this scenario. Other example is the news feed of your facebook. If your friend is posting something then it automatically be displayed to your news feed page, that is what I'm trying to get...I hope somebody can get my point. Thanks in advance.
Here is the "cool" stuff for realtime web in. Net: http://signalr.net/
You can make an AJAX call every, let's say, 5 seconds.
If you are using Jquery in your site, you can try:
var seconds = 5;
setInterval(function(){
$.ajax({
url: urlToHandler,
data: jsonData,
dataType: 'json',
type: 'POST',
contentType: 'application/json',
success: function(data) {
$('#container').html(data.html)
},
error: function(data, status, jqXHR) {
console.log('There was an error.');
}
}); // end $.ajax
}, seconds * 1000)
I am very new into ajax and i am having a strange problem, I have created an asp.net website that have a web service within the project I am calling this web service by following ajax code:
$.ajax({
type: "POST",
url: pageUrl + "/PassData",
data: JSON.stringify({ aos_code: code }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccessCall
});
the c# code is not very complicated, basically it will get the code and call the stored procedure and return some value:
[WebMethod(EnableSession = true)]
public string[] PassData(string aos_code)
{
// calling the database and returning some data
return data;
}
And it works fine; the problem is that if two different users accessing the application in the two different computers and let say they clicking a button at the same time to call the server side function via Ajax, it will return the “500 internal server error”.
I am not sure how exactly Ajax work, is request are belong to same session or all user requesting a single function?
Hope it makes sense, and I would really do appreciate for the clarification and help.
Regards,
Error 500 indicates that something went wrong on the server side. Check your console (firebug or similar) to see what exactly happened and try to fix it. Ajax has nothing to do with it, it is simply as if two users visited the same page at the same time.