Not able to fetch data/Response on Success of Jquery.Ajax() - javascript

Hi people, I am craving myself from past 3 days and I just couldn't find the way to access json response seen on my browser
Here is my Ajax code :
$("[id*=btnModalPopup]").live("click", function () {
$("#tblCustomers tbody tr").remove();
$.ajax({
type: "POST",
url: "CallDataThroughJquery.aspx/GetLeadDetails",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert("Hi Json");
alert(data.Leadno); // **Says data.leadno is undefined**
response($.map(data.d, function (item) { // **here I am going some where wrong**
//**cannot catch response. Help!**
}))
},
failure: function (response) {
alert(response.d);
}
});
});
Please help me on this.. Thanks in Advance!

I see that your JSON is an array with an object. Try data[0].Leadno

$.ajax({
type: "POST",
url: "CallDataThroughJquery.aspx/GetLeadDetails",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert("Hi Json");
alert(data.d[0]['Leadno']); // **Says data.leadno is undefined**
response($.map(data.d, function (item) { // **here I am going some where wrong**
//**cannot catch response. Help!**
}))
},
failure: function (response) {
alert(response.d);
}
});
Try your alert with 'data.d[0]['Leadno']'.

Related

Toggle switch to call url but not open it

I have a toggle switch on my html which i would like to enable/disable a relay in arduino with.
On: call http://192.168.1.144:8000/1234!Q02=0$
Off: call http://192.168.1.144:8000/1234!Q02=1$
I don't want the url to be opened, i just want it to be called.
function getValue1() {
var isChecked = document.getElementById("button1").checked;
if(isChecked){
alert("button1 is checked");
} else {
alert("button1 is NOT checked");
}
}
My javascript code is working, but i dont know how to call the url.
Thanks.
you can also try this.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#btnCall").click(function(){
if($("button1").prop('checked')){
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://192.168.1.144:8000/1234!Q02=0$",
data: JSON.stringify(data), //yourData
success: function (data) {
console.log(data);
},
dataType: "json"
});
} else {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://192.168.1.144:8000/1234!Q02=1$",
data: JSON.stringify(data), //yourData
success: function (data) {
console.log(data);
},
dataType: "json"
});
}
});
});
</script>
Button:
$(document).ready(function() {
$("#btnCall").click(function(){
var isChecked = document.getElementById("button1").checked;
if(isChecked){
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://192.168.1.144:8000/1234!Q02=0$",
data: JSON.stringify(data), //yourData
success: function (data) {
console.log(data);
},
dataType: "json"
});
} else {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://192.168.1.144:8000/1234!Q02=1$",
data: JSON.stringify(data), //yourData
success: function (data) {
console.log(data);
},
dataType: "json"
});
}
});
});

Getting data from javascript function on to ASP.NET MVC Controller's Action Method

I have looked at many posts on SO to figure out my situation but with not much success. I'm trying to send data from a javascript function(DropdownChange()) to MVC controllers Action method as a parameter based on the dropdown value that is selected on the view. Here is my code:
In .cshtml
//code for dropdown
....followed by
//code for creating a new plan
<a href="#Url.Action("ActionMethod")">
<i class="sth" id="sth" Title="Create"></i>
</a>
In .JS file, function to get dropdown change value
function DropdownChange(){
var Value = parseInt($("#dropDownId").val());
$.ajax({
type: "POST",
url: '# Url.Action("ActionMethod", "Home")',
contentType: 'application/json; charset=utf-8',
dataType: "json",
data: {data : JSON.Stringify(Value)},
success: function (data) {
alert('success');
},
error: error: function (result){
alert('error');
}
});
and Here is my Controller action method
[HttpPost]
public ActionResult ActionMethod(string Value)
{
// do something with Value
}
All I'm getting is an error alert message and a JS runtime error along with a null value for Value parameter. Can any one help me in this scenario.
Thanks in advance.
Try
$.ajax({
type: "POST",
url: '# Url.Action("ActionMethod", "Home")',
contentType: 'application/json; charset=utf-8',
dataType: "json",
data: JSON.stringify({data: Value}),
success: function (data) {
alert('success');
},
error: error: function (result){
alert('error');
}
});
OR
$.ajax({
type: "POST",
url: '# Url.Action("ActionMethod", "Home")',
contentType: 'application/json; charset=utf-8',
dataType: "json",
data: Value,
success: function (data) {
alert('success');
},
error: error: function (result){
alert('error');
}
});

how to get the json string in a variable

i got my json string inside the ajax as function like this way
$.ajax({
type: "POST",
url: "http://localhost/./Service/GetPageInfo",
dataType: "json",
contentType: 'application/json',
data: JSON.stringify({
filename: filename
}),
success: function (data) {
alert('Success');
},
error: function () {
alert('Error');
}
});
here i get data like
[{"main":{"sub":[],"tittle":"manu","startvalue":"","stopvalue":"","status":"","accumalated":"","comment":""}}]
i want it in a variable like
var myjsonobject =[{"main":{"sub":[],"tittle":"manu","startvalue":"","stopvalue":"","status":"","accumalated":"","comment":""}}]
There you go :
$.ajax({
type: "POST",
url: "http://localhost/./Service/GetPageInfo",
dataType: "json",
contentType: 'application/json',
data: JSON.stringify({
filename: filename
}),
success: function (data) {
alert('Success');
var jsonobject = data;
},
error: function () {
alert('Error');
}
});
Also I strongly advise you to use promises to make API calls: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Promise
var jsonobject= null;
$.ajax({
type: "POST",
url: "http://localhost/./Service/GetPageInfo",
dataType: "json",
contentType: 'application/json',
data: JSON.stringify({
filename: filename
}),
success: function (data) {
jsonobject=data;
alert('Success');
},
error: function () {
alert('Error');
}
});
If you want wait for ajax response and fill up variable then pass async: false in ajax request options.
Based on your comment, you need to parse the JSON in your success handler,
success: function (data) {
alert('Success');
var myjsonobject = JSON.parse( data );
},

$ajax method does not execute for external javascript

very stupidly question I think, When I put this code inline then works, but when I put it on external js file it can not debug after the $ajax method.
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: 'WebService/Common/Bank.asmx/GetBank',
data: "{}",
dataType: "json",
async: false,
success: function (data) {
debugger; ///debug point not reach here
$.each(data.d, function (key, value) {
$("#ddlLCMasterBank").append($("<option></option>").val(value.BankID1).html(value.BankName1));
});
},
error: function (result) {
alert("Error");
}
});

How to get data with jQuery ajax?

I'm try using jQuery Ajax for popularize a jqxGrid, but is not success
my jQuery code:
$.ajax(
{
url: "Cliente.aspx/GetClient",
type: "POST",
data: {},
dataType: "json",
success: function (msg) {
alert(msg);
},
error: function(msg) {
alert("error"); //msg is returning error
}
});
I'm try get data with Entity Framework
[WebMethod]
public static string GetClient()
{
string dados;
using (SysContext db = new SysContext())
{
dados = new JavaScriptSerializer().Serialize(db.Clients.ToList());
}
return dados;
}
Where is my error? why
Look over here: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
You are missing:
contentType: "application/json; charset=utf-8",
Which is needed to go to the Webmethod. So it will be:
$.ajax(
{
url: "Cliente.aspx/GetClient",
type: "POST",
data: {},
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (msg) {
alert(msg);
},
error: function(msg) {
alert("error"); //msg is returning error
}
});

Categories