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
Related
When I want to send data as datatype json it response this error:
SyntaxError: Unexpected token < in JSON at position 0
When I tried to send the data as datatype text, it sends the data to php successfully but my php wont respond.
ajax/js as datatype json:
let form = $("#form");
$("#form").on("submit", function(e) {
e.preventDefault();
$.ajax({
url: "test.php",
method: "POST",
dataType: "json",
data: {
test: 1
},
success: function (r) {
console.log("!!!");
},
error: function(jqXHR, textStatus, errorMessage) {
console.log(errorMessage);
}
});
});
ajax/js as datatype normal:
let form = $("#form");
$("#form").on("submit", function(e) {
e.preventDefault();
$.ajax({
url: "test.php",
method: "POST",
data: form.serialize(),
success: function (r) {
console.log("!!!");
},
error: function(jqXHR, textStatus, errorMessage) {
console.log(errorMessage);
}
});
});
php code:
if(isset($_POST["test"])){
echo "<script>console.log('works');</script>";
}
Try this
var ob = {
test: 1
};
$.ajax({
url: "test.php",
method: "POST",
dataType: "json",
contentType: 'application/json',
data:JSON.stringify(ob),
success: function (r) {
console.log("!!!");
},
error: function(jqXHR, textStatus, errorMessage) {
console.log(errorMessage);
}
});
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?
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 );
},
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 wrote this code:
(function($) {
var url = 'http://surfujpametno.roditelji.me/2014/01/23/surfujpametno-aplikacija-za- android/?json=get_all_posts&callback=?';
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'callback',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
console.log(json.status);
console.log(json);
try{
json = $.parseJSON(json);
alert(json);
}catch(e){
alert('invalid');
}
},
error: function(e) {
console.log(e.message);
}
});
})(jQuery);
http://surfujpametno.roditelji.me/2014/01/23/surfujpametno-aplikacija-za-android/?json=get_all_posts&callback=?
That is JSON url
I get 'Invalid'... If I remove try and catch i get this error: http://prntscr.com/2sf8bd
Try this, Removed $.parseJSON, because of dataType: 'jsonp', already defined
(function($) {
var url = 'http://surfujpametno.roditelji.me/2014/01/23/surfujpametno-aplikacija-za-android/?json=get_all_posts&callback=?';
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'callback',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
console.log(json.status);
console.log(json);
},
error: function(e) {
console.log(e.message);
}
});
})(jQuery);
As Paulloz indicated your json is already a JSON object: JSfiddle without parse and alert: http://jsfiddle.net/Skadi2k3/cKUD7/