$(document).ready(function() {
$.ajax({
type: "GET",
dataType: "json",
contentType: "text/plain",
xhrFields: {
withCredentials: true
},
url: 'http://kitabisa.xyz/api/v2/campaigns',
username: 'apps1',
password: 'XD6WVjhcq3JVrFRldrpjEpAyUXg5LFzS9cmiGuXL3TmE7bkLGR',
success: function(result) {
alert('done');
},
complete: 'callback'
})
});
I want to GET JSON from API via jquery Ajax function, above is my code snippet, but the CORS Policy block the ajax, here is the console message.
I've tried the another method, I use beforeSend ajax here :
$(document).ready(function() {
$.ajax({
type: "GET",
dataType: "jsonp",
contentType: "text/plain",
xhrFields: {
withCredentials: true
},
url: 'http://kitabisa.xyz/api/v2/campaigns',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic YXBwczE6WEQ2V1ZqaGNxM0pWckZSbGRycGpFcEF5VVhnNUxGelM5Y21pR3VYTDNUbUU3YmtMR1I=")
},
username: 'apps1',
password: 'XD6WVjhcq3JVrFRldrpjEpAyUXg5LFzS9cmiGuXL3TmE7bkLGR',
success: function(result) {
alert('done');
}
})
});
But still not works.
Any suggestion to fix this problem? Thank you
try the following
$.ajax({
type: "GET",
dataType: "json",
contentType: "text/plain",
xhrFields: {
withCredentials: true,
username: 'apps1',
password: 'XD6WVjhcq3JVrFRldrpjEpAyUXg5LFzS9cmiGuXL3TmE7bkLGR'
},
url: 'http://kitabisa.xyz/api/v2/campaigns',
success: function(result) {
alert('done');
},
});
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 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"
});
}
});
});
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);
});
}
});
This code gets a json object from another server.
How do I access the response outside of the function?
(function($) {
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'callback',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
var data = json;
}
})
})(jQuery);
You can do something like this!
_outerMethod: function(data) {
//...do what you need to do
}, (function($) {
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'callback',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
_outerMethod(json);
}
})
})(jQuery);
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) {
}
});