Jquery Ajax Call to an ASP.NET Webmethod

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



Leave a reply



Submit