I have seen the previous questions asked by others in stack overflow.but did'nt work for me.Iam getting the data in json when seen in the network panel,but
var jsonData = {
"name": fbUsername,
"email": fbEmail
};
console.log(jsonData);
$.ajax({
type: "get",
contentType: "application/json",
data: jsonData,
jsonp: false,
jsonCallback: 'jsonCallback',
dataType: 'jsonp',
crossDomain: true,
headers: { "api_key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" },
url: "https://xaxxxxxxxxxx/prod/users",
success: function(res) {
alert("success!");
},
error: function(jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
});
function jsonCallback(results){
alert(results);
}
PARSE ERROR
I am getting the response in JSON
['name1','name2']
although the response is JSON and why it is coming an parse error?
Related
My code do not response any data to me..
bellow some code:
<script type="text/javascript">
function matriculaFn(mat) {
$.ajax({
method: 'GET',
url:'My url API,
async: true,
crossDomain: false,
contentType: 'application/json;charset=utf-8',
dataType: 'json',
headers: {
"Authorization": "Token xxxprivatexxxxx"
},
options: {
useSSL: true
},
success: function(data) {
console.log(data);
var dados = JSON.stringify(data);
console.log(dados);
$("#IDDOCAMPO").val(data.AlgumaCoisa);
},
//error : function(xhr, status, error) {
// do stuff here
//var data = jQuery.getJSON(xhr.responseText);
//console.log(data);
//}
});
};
</script>
here is the error:
jquery-3.3.1.min.js:2 OPTIONS https://url net::ERR_CERT_DATE_INVALID
Can't comment yet because I don't have enough rep but it looks as though this error is Chrome specific. Have you tried other browsers? If so, try some of the methods listed here: link
Other things you could try are:
crossDomain: true,
or adding:
xhrFields: {
withCredentials: true
},
or because it's a GET request try:
dataType: 'jsonp',
I'm using a jquery ajax for storing a data in the database. I call a service through a url and send data to it in the following way:
$.ajax({
type: "POST",
url: "http://192.168.250.118:8080/rest_service/rest/user",
data: JSON.stringify({ "loginName": "tsample#gmail.com", "mobile": "1234567890" }),
async: false,
contentType: "application/json",
crossDomain: true,
success: function(data){
alert("success");
},
error: function(XMLHttpRequest, textStatus, errorThrown){
console.log(XMLHttpRequest);
console.log(textStatus);
console.log(errorThrown);
}
});
When I execute this, I get this error:
{"readyState":0,"status":0,"statusText":"error"}
This is what I get when I look for errors in network:
But, if I try using postman, the data is getting stored and I get 200OK response. What's wrong with the above code? What should I change?
Stringify the data before sending - data: JSON.stringify({ "loginName": "tsample#gmail.com", "mobile": "1234567890" }) amd contentType:"application/json"
$.ajax({
type: "POST",
url: "http://api.xyz.com/go/login",
contentType: "application/json; charset=utf-8",
crossDomain: true,
dataType: "json",
data:{id:'547',password:'password'},
success: function (data, status, jqXHR) {
alert("success");// write success in " "
},
error: function (jqXHR, status) {
// error handler
console.log(jqXHR);
alert('fail' + status.code);
}
});
This is the sample of code I used to get response.
what's wrong here?
The code isn't responding json file.
I am making an AJAX request to a PHP controller, by using jQuery ajax, but when trying to get the posted data with PHP the $_POST is empty. Below is the actual function:
function GetSeriesForManufacturer(manuf) {
selectedmanufacturer = manuf;
//Make an AJax Call For Getting Series Of Manufacturer
var series = null;
$.ajax({
type: "POST",
url: url,
data: "{manufacturer:'" + selectedmanufacturer + "'}",
contentType: "application/json", //; charset=utf-8",
dataType: "json",
cache: false,
async: false,
success: function (response) {
//remove loading gif
$(".loading").hide();
//Append Data
AppendSeries($.parseJSON(response.text), selectedmanufacturer);
//Custom Scrollbar Call
$('.MatchingSeries ul').mCustomScrollbar();
},
error: function (XMLHttpRequest, textStatus, errorThrown) { }
});
}
Thanks in advance!
First, you don't need to stringify data. Just send object literal is ok.
data: {manufacturer: selectedmanufacturer},
Second, you don't need this line:
contentType: "application/json",
Let jQuery do the encoding for you:
$.ajax({
type: "POST",
url: url,
data: {
manufacturer: selectedmanufacturer
},
contentType: "application/json", //; charset=utf-8",
dataType: "json",
cache: false,
async: false,
success: function (response) {
...
},
error: function (XMLHttpRequest, textStatus, errorThrown) { }});
I am executing this code:
var element=null;
$.ajax({
type: 'GET',
async: false,
url: "C:\Users\myDir\Desktop\Project\jsonfile.json",
dataType: 'json',
success : function(data) {
element=data;
}
});
JSON structure:
{
"info":[
{
"a1": "Ram",
"b1": "P123"
},
{
"a1": "ROM",
"b1": "P245"
}
]
}
but i am getting nothing in variable
Check for any error in the ajax using
var element=null;
$.ajax({
type: 'GET',
async: false,
url: "jsonfile.json",//Edited
dataType: 'json',
success : function(data) {
element=data;
}
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
may be the permission issue for the file. insert the json file with all your other code files and give relative path in the url
$.ajax({
type: 'GET',
async: false,
url: "jsonfile.json",
dataType: 'json',
success : function(data) {
element=data;
}
});
//location of json file is same as the file making ajax call