jQuery AJAX call to an ASP.NET WebMethod
You should really make sure to properly encode this JSON. JSON.stringify
is the most reliable method:
data: JSON.stringify({ accessCode: code, newURL: url })
This ensures that even if the code
and url
variables contain some dangerous characters that will break your string concatenations in the resulting JSON at the end everything will be properly encoded. The JSON.stringify method is natievly built into modern browsers but if you need to support legacy you could include json2.js.
Also because you code is no longer blocking you should make sure that if you call this sendUpdate
from some button click or form submit you cancel the default action by returning false.
Jquery Ajax Call to WebMethod in asp.net
You should make WebApi instead, if you expect JSON or XML with jquery.ajax
Check youtube, for example this one
https://www.youtube.com/watch?v=6qwuFQDB2jU&index=2&list=PL6n9fhu94yhW7yoUOGNOfHurUE6bpOO2b
jquery.ajax calling a .aspx web method
Your ajax should look like this. Remove the '=' and replace with ':' to create proper json in the data field.
$.ajax({
type: "POST",
url: "SendEmail.aspx/SendMyEmail",
data: '{EmailFromAddress: "mike", EmailFromName: "mike", EmailSubject: "email subject here", EmailBody: "email body here"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
ASP .NET call Webmethod with ajax request from javascript
I'm not sure if your code structure was intended for the purpose of the Q, If not, then you may need start by decoupling and focus on a key element of "ContentType" in your AJAX request.
Start by adding a .asmx (which is what [WebMethod] refers too) file to your solution.
Then add the method:
[WebMethod]
public string HelloWorld()
{
return "Hello, World";
}
Now assuming your using an external js sheet, apply the following logic (Onload or OnClick):
$.ajax
({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/webservice-folder/webservice.asmx/HelloWorld",
success: (function (data) {
alert(data.d)
}),
error: (function () {
alert("error");
})
});
Ajax Call in Asp.net ([WeBMethod] Function not calling)
Make your [WebMethod]
static as
[WebMethod]
public static void Test(string str)
{
//Console.WriteLine(str);
string retstr=str;
}
change ajax data value to data: "{'str':'" + text + "'}"
UPDATE
Try This Same Code
aspx:
<asp:Button ID="Button1" ClientIDMode="Static" runat="server" Text="Button" />
aspx.cs
[WebMethod]
public static void Test(string str)
{
string abc=str;//Use this wherever you want to, check its value by debugging
}
test.js
$(document).ready(function () {
$("#Button1").click(function () {
var text = "this is test";
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Test.aspx/Test",
data: "{'str':'" + text + "'}",
dataType:"json",
success: function (data) {
alert("yes");
},
error: function (response) {
alert(response);
}
});
});
});
Related Topics
Datagridview: How to Set a Cell in Editing Mode
How to Read/Stream a File Without Loading the Entire File into Memory
Newtonsoft JSON.Net Serialize Jobject Doesn't Ignore Nulls, Even with the Right Settings
Pass a Value from One Form to Another
Caliburn.Micro Serialization Issue When Implementing Propertychangedbase
Why \B Does Not Match Word Using .Net Regex
When Would You Use Delegates in C#
Datetimepicker: Pick Both Date and Time
How to Force My C# Winforms Program Run as Administrator on Any Computer
Is the Size of a Form in Visual Studio Designer Limited to Screen Resolution
Writing String at the Same Position Using Console.Write in C# 2.0
How to Show the "Open With" File Dialog
How to List Available Video Modes Using C#
Unity - Checking If the Player Is Grounded Not Working
How to Create a Button That Can Send Keys to a Conrol Without Losing Focus - Virtual Keyboard
When Should I Choose Inheritance Over an Interface When Designing C# Class Libraries