jquery ajax code is not passing values to webservice method - c#

My ajax code is not passing values to my webservice method .. i think i am not doning it properly. please guide me.
this is my .aspx code:
$(function () {
$.ajax({
type: "POST",
url: "WebService.asmx/InsertRediretTime",
data: "{ 'ReachTime': '21-Nov-11', 'Destination': 'location' }",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data, status) {
alert(data.d);
}
});
});
and this is my webservice method
public static void InsertRediretTime(string ReachTime, string Destination)
{
//operational code
}
Thanks in advance

Take out the static keyword from your method.
public void InsertRediretTime(string ReachTime, string Destination)
{
//operational code
}

Try this:
$(function () {
$.ajax({
type: "POST",
url: "WebService.asmx/InsertRediretTime",
data: "ReachTime=21-Nov-11&Destination=location",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data, status) {
alert(data.d);
}
});
});
Although your service is expecting id and order, but you're passing ReachTime and Destination - is this correct?

What is the error that you are getting? See tool such as Fiddler (or Firebug on Firefox) to inspect the request/response - see the response for your ajax request - that will help you to troubleshoot the issue.
OTH, you need a ScriptService attribute applied to your web service class. If you are using .NET 2.0/3.5 then you also need configuration entries to register ScriptHandlerFactory handler that is responsible for JSON support in asmx services. See this article for more info regarding configuration: http://encosia.com/asmx-scriptservice-mistakes-installation-and-configuration/

Thanks everyone for your help ... a combination of your help worked for me .. here is the solution:
$(function () {
$.ajax({
type: "POST",
url: "WebService.asmx/InsertRediretTime",
data: '{ ReachTime: "21-Nov-11", Destination: "location" }',
contentType: 'application/json; charset=utf-8',
dataType: JSON,
success: function (data, status) {
alert(data.d);
}
});
});
and,
public void InsertRediretTime(string ReachTime, string Destination)
{
blah blah
}
Thanks again :)

Try this,
in aspx page
$(function () {
$.ajax({
type: "POST",
url: "WebService.asmx/InsertRediretTime",
data: '{ReachTime:21-Nov-11,Destination:location}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data, status) {
alert(data.d);
}
});
});
In webservice
public string InsertRediretTime(string ReachTime, string Destination)
{
//operational code
return stringData;
}

Related

Trying to redirect from AJAX post

I am trying to call a webmethod from AJAX which is redirecting to a url like this:
HttpContext.Current.Response.Redirect(StoreCode.ToLower() + "/App/home.html?EventClick=True", false);
AJAX call:
function TokenPost() {
var token = $('#hdnToken').val();
$.ajax({
url: 'http://localhost/QDC/WebServices.asmx/ChkToken',
type: 'POST',
data: "{data:'" + token + "'}",
contentType: 'application/json; charset=UTF-8',
datatype: 'JSON',
success: function(response) {
},
error: function(response) {
alert(response.d);
}
});
}
But my AJAX call is not redirecting to that url.Thanks in advance.
I think it' s not possible in HttpPost Methods. But you can return link in your HttpPost method from server. And redirect with Javascript Code if ajax returns success.
$.ajax({
url: 'http://localhost/QDC/WebServices.asmx/ChkToken',
type: 'POST',
data: "{data:'" + token + "'}",
contentType: 'application/json; charset=UTF-8',
datatype: 'JSON',
success: function (link) {
window.location.href = link;
},
error: function (link) {
alert(response.d);
}
});
In Server Method (I am using this in ASP.NET MVC):
string link = "https://...Your Link";
return Json(link, JsonRequestBehavior.AllowGet);
If you are using WebMethod, I think you can return just string, too.
[System.Web.Services.WebMethod]
public static string ChkToken(string data)
{
string link = "...";
// Do things
return link;
}
This time ajax return can be like this,
success: function (link) {
window.location.href = link.d;
},

Accessing PageMethod from aspx page

I have a webforms application and need to make an jquery ajax call to a PageMethod (i.e. WebMethod) in code behind from my aspx page. So far it does not work for me. Is this possible?
Here is my code:
$(function()
{
setInterval(function(){
$.ajax({
type: "GET",
url: "/ClientBillingBillDetails.aspx/MyPageMethod",
data: {someData: '<%= TheData %>'},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result) {
}
});
}, 10000);
});
[System.Web.Services.WebMethod]
public static string MyPageMethod(int someData)
{
return "";
}
Is something wrong with my URL or something else?
Thanks
Try This:
$(function () {
setInterval(function () {
$.ajax({
type: "POST",
url: "/ClientBillingBillDetails.aspx/MyPageMethod",
data: "{ 'someData': '<%= TheData %>' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
}
});
}, 10000);
});
Use type as post and make sure if you have ajax.jquery library reference added in solution.
Also i think you can remove '/' in specifying method..
Just use "ClientBillingBillDetails.aspx/MyPageMethod".
Else you can use simple pageMethods using scriptmanager

I have internal server error 500 when I tried to post json in Jquery ajax

When I try to send json JSON.stringify(coords); or above code I get error message but I try when the data is empty like data:{} the code works correctly. How can I solve this problem?
$.ajax({
type: "POST",
data: {"yagiz":"aabb"},
dataType: 'json',
url: url,
crossDomain:true,
async: false,
contentType: "application/json; charset=utf-8",
success: function (response) {
$("#Content").text(response.d);
console.log(response.d);
},
failure: function (response) {
alert(response.d);
console.log(response.d);
}
});
Web Method
[System.Web.Services.WebMethod]
public static string GetLocationPolygon(string location)
{
return location;
}
You can debug on server side as 500 internal error raised at server side only. You can catch and log the exact exception.
try this code
$.ajax({
type: "POST",
data: JSON.stringify({"location":"aabb"}), //Change here
dataType: 'json',
url: url,
crossDomain:true,
async: false,
contentType: "application/json; charset=utf-8",
success: function (response) {
$("#Content").text(response.d);
console.log(response.d);
},
failure: function (response) {
alert(response.d);
console.log(response.d);
}
});

Ajax URL - Incorrect .net #c

Been following some tutorials, as i'm learning C#, however was just attempting to make an ajax call to the server, i'm using my localhost.
As far as I can tell i'm doing its right, but it's obviously not.
Maybe it my folder structure or just the name of the file.
The Default.aspx file is at the project root
Here is my ajax call
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx",
dataType: "json",
data: "{'FirstName':'test','LastName':'test1','City':'test3','EmailID':'test4'}",
success: function (data) {
console.log("Woo!");
},
error: function (result) {
console.log("fail!");
}
});
I know eventually it will have to call a method within the file, but its not even finding it at the moment.
Thanks in advance, James
You can use controller Home and create the one action.
Example:
public ActionResult FirstAjax()
{
return Json(true, JsonRequestBehavior.AllowGet);
}
After that your jquery ajax is:
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "Home/FirstAjax",
dataType: "json",
data: "",
success: function (data) {
console.log("Woo!");
},
error: function (result) {
console.log("fail!");
}
});

Jquery to ashx not working event not fired

I am using the below code to call a ashx page. But it's not working for me. I have place my code here. I always got the error message message "Request Failed". Please help me..
<script type="text/javascript">
function CallLoginHandler(user, pass) {
alert(user);//Got value
alert(pass);//Got Value
$(function(){
$.ajax({
type: "POST",
url: "../handler/JQGridHandler.ashx?MainPage=GetUserDetails&Type=2&user=" + user + "&pass=" + pass + "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnComplete,
error: OnFail
});
return false;
});
}
function OnComplete(result) {
alert([result.Id, result.Name, result.Age, result.Department]);
}
function OnFail(result) {
alert('Request Failed');
}
</script>
remove these lines:
$(function(){ // <----remove this
return false; // and this
}); // and this too
Update to this function:
function CallLoginHandler(user, pass) {
$.ajax({
type: "POST",
url: "../handler/JQGridHandler.ashx", // updated url
data: { // pass your data like this since type is post
MainPage:"GetUserDetails",
Type:2,
user:user,
pass:pass
},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnComplete,
error: OnFail
});
}
and also your url is not correct:
url: "../handler/JQGridHandler...... + pass + "",
// --here you have a blank `""` after pass-----^
and since your type: "post" so you can pass the data like this:
data: {
MainPage:"GetUserDetails",
Type:2,
user:user,
pass:pass
},
Separate your string into a javascript object to be encoded and sent to the server as JSON based on your content type. Also, get rid of $(function(){ in your function call, it's useless here.
$.ajax({
type: "POST",
url: "../handler/JQGridHandler.ashx';
contentType: "application/json; charset=utf-8",
dataType: "json",
data: {
MainPage: 'GetUserDetails',
Type: 2,
user: user,
pass: pass
}
success: OnComplete,
error: OnFail
});
If this doesn't work, then the issue is likely one of the following:
The URL address is wrong
The server is evaluating a GET as opposed to a POST request
The server expects, application/x-www-form-urlencoded but you've declared it's json
You have a routing issue
Note: Do not send your user and pass in query string.
function CallLoginHandler(user, pass) {
$.ajax({
type: "POST",
url: "../handler/JQGridHandler.ashx",
data: {
MainPage: 'GetUserDetails',
Type: 2,
user: user,
pass: pass
},
// DO NOT SET CONTENT TYPE to json
// contentType: "application/json; charset=utf-8",
// DataType needs to stay, otherwise the response object
// will be treated as a single string
dataType: "json",
success: OnComplete,
error: OnFail
});
});
}
Your handler.ashx file
using System;
using System.Web;
using Newtonsoft.Json;
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string user= context.Request.Form["user"];
//.....
var wrapper = new { d = myName };
// in order to use JsonConvert you have to download the
// Newtonsoft.Json dll from here http://json.codeplex.com/
context.Response.Write(JsonConvert.SerializeObject(wrapper));
}
public bool IsReusable
{
get
{
return false;
}
}
}
source

Categories