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"
});
}
});
});
Related
I have a post request from jquery ajax to a ActionResult method as follow :
$("#itemTextbox, #itemTextboxNew, #quantity1Textbox").on("keydown", function (e) {
if ((e.keyCode == 120){
var GetReportLast5SellAndBuyURL="/Trd/SellInvoice/GetReportLast5SellAndBuy";
itemCode = $(this).val();
$.ajax({
type: "POST",
url: GetReportLast5SellAndBuyURL,
data: {ItemCode:itemCode},
//contentType: "application/json; charset=utf-8",
context: this,
processData: false
}).done(function (msg) { ... somethings ...});}
And in controller, ActionResult is :
[HttpPost]
public ActionResult GetReportLast5SellAndBuy(string ItemCode)
{ ... somthings ...}
But when ActionResult is called " ItemCode " is null... What's wrong with this chapter?
I tried different forms of this recipe, but the problem is still there..
try this:
$.ajax({
type: "POST",
url: GetReportLast5SellAndBuyURL,
data: JSON.stringify({ItemCode:itemCode}),
datatype: "JSON",
contentType: "application/json; charset=utf-8",
processData: false
}).done(function (msg) { ... somethings ...});}
Just comment processData:false in your script
$("#itemTextbox, #itemTextboxNew, #quantity1Textbox").on("keydown", function (e) {
if ((e.keyCode == 120){
var GetReportLast5SellAndBuyURL="/Trd/SellInvoice/GetReportLast5SellAndBuy";
itemCode = $(this).val();
$.ajax({
type: "POST",
url: GetReportLast5SellAndBuyURL,
data: {ItemCode:itemCode},
//contentType: "application/json; charset=utf-8",
context: this
// processData: false
}).done(function (msg) { ... somethings ...});}
well explained at [Setting processData to false in jQuery breaks my AJAX request
$("#itemTextbox, #itemTextboxNew, #quantity1Textbox").on("keydown", function (e) {
if ((e.keyCode == 120){
$.ajax({
type: "POST",
url: "/Trd/SellInvoice/GetReportLast5SellAndBuy?ItemCode="+$(this).val(),
contentType: "application/json",
context: this,
datatype: "JSON",
processData: false
}).done(function (msg) { ... somethings ...});}
// OR
$("#itemTextbox, #itemTextboxNew, #quantity1Textbox").on("keydown", function (e) {
if ((e.keyCode == 120){
$.ajax({
type: "POST",
url: "/Trd/SellInvoice/GetReportLast5SellAndBuy",
contentType: "application/json",
data:JSON.stringify({ItemCode:$(this).val()})
datatype: "JSON",
context: this,
processData: false
}).done(function (msg) { ... somethings ...});}
I have a json which is . I just want to get specific data which is
obj['contacts']['name']
How can i get
obj['contacts']['name']
name on Contacts array
This is my code:
$.ajax({
type: 'GET',
dataType: 'json',
url: uri,
cache: false,
contentType: 'application/json',
success: function(data) {
for (var obj in data) {
console.log(obj['contacts']['name']);
}
}
});
In your case this is how you want get name from contacts
$.ajax({
type: 'GET',
dataType: 'json',
url: uri,
cache: false,
contentType: 'application/json',
success: function(data) {
if (!data.contacts) return;
var names = data.contacts.map(function(dt) {
return dt.name;
});
console.log(names);
}
});
Just enumerate the returned object "contacts" property:
$.ajax({
type: 'GET',
dataType: 'json',
url: uri,
cache: false,
contentType: 'application/json',
success: function(data) {
data.contacts.forEach(function(contact) {
console.log(contact.name);
});
}
});
I have a post statement,
$.post("panel.php", 'data=[{"action":"UserInfo"}]', function (userInfo){
//processing
});
I need it to be converted to $.ajax so made it thus,
$.ajax({
type: "POST",
url: "panel.php",
data: { data: [{"action":"UserInfo"}]},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(userInfo) {
//processing
}
});
But the post variable isn't being sent. Is this not the correct way?
Can you try something like this:
$.ajax({
type: "POST",
url: "panel.php",
data: {"action":"UserInfo"},
success: function(userInfo) {
//processing
}
});
Try this
$.ajax({
type: "POST",
url: "panel.php",
data: "action="+"UserInfo",
success: function(userInfo) {
//processing
}
});
Remove data from your data and keep it in a variable, stringify before your send as below
var data={"action":"UserInfo"};
$.ajax({
type: "POST",
url: "panel.php",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(userInfo) {
//processing
}
});
Your data attribute was not written as correct JSON:
data: { "data": [{"action":"UserInfo"}]},
You need quotation marks around the items inside your JSON object. You can use JSONLint to check if your JSON object is valid.
$.ajax({
type: "POST",
url: "panel.php",
data: {"action":"UserInfo"},
dataType: "json",
success: function(userInfo) {
//processing
}
});
Need a small change. there is a predefined format to send data in ajax,
data: {status: status, name: name},
data: "status="+status+"&name="+name.
Follow any one of the approach.
try like this,
$.ajax({
type: "POST",
url: "panel.php",
data: {"action":"UserInfo"},
success: function(userInfo) {
}
});
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']'.
I'm trying to delete a row of data using JSON, however when I prompt a confirm dialog my javascript function doesn't work as follows:
<script type="text/javascript">
$().ready(function() {
$("a.delete").click(function() {
$.ajax({
type: "POST", contentType: "application/json; charset=utf-8", url: this.href, data: "{}", dataType: "json",
success: function(msg) {
if (msg.status == "ok") {
$("tr#" + msg.id).hide();
}
else {
alert(msg.exception);
}
}
});
return false;
});
});
</script>
The above works absolutely fine, but the minute I put the following in:
<script type="text/javascript">
$().ready(function() {
$("a.delete").click(function() {
if (!confirm("Are you sure you want to delete this?")) return false;
$.ajax({
type: "POST", contentType: "application/json; charset=utf-8", url: this.href, data: "{}", dataType: "json",
success: function(msg) {
if (msg.status == "ok") {
$("tr#" + msg.id).hide();
}
else {
alert(msg.exception);
}
}
});
return false;
});
});
</script>
This does carry out the delete, but it doesn't hide the table row, which makes me think it hasn't been deleted. Any ideas?
Try this:
<script type="text/javascript">
$().ready(function() {
$("a.delete").click(function() {
if (confirm("Are you sure you want to delete this?")){
$.ajax({
type: "POST", contentType: "application/json; charset=utf-8", url: this.href, data: "{}", dataType: "json",
success: function(msg) {
if (msg.status == "ok") {
$("tr#" + msg.id).hide();
}
else {
alert(msg.exception);
}
}
});
}
return false;
});
});
</script>
hope it works for you...