Javascript Json Not Working - javascript

I've tried it, but I haven't found a solution.
$(document).ready(function(){
$.ajax({
url: 'https://api.firsatbufirsat.com/deal/browse.html?apiKey=FZTGT8NK3RTC5M2O&publisherId=609&cityId=42',
type: 'get',
dataType: "jsonp",
success: function(data){
if(data){document.write(data.deals[0].title)}
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Why doesn't this work?

Related

jquery ajax function not do anything

all Ajax not working in my project,
my structure project is
http://localhost/aaa/beranda.php?h=Add-Pengeluaran
The jquery code is as follows
<script type="text/javascript">
$(document).ready(function() {
$('#tampil').load("tampil.php");
$("#Submit").click(function() {
var data = $('#form').serialize();
$.ajax({
type: 'POST',
url: "insert.php",
data: data,
cache: false,
success: function(data) {
$('#tampil').load("tampil.php");
}
});
});
});
</script>
please help me

Issue with nested ajax call in jQuery

I have a situation where I make an ajax call, on returning from which (successfully), I make an another ajax call with the returned data and submit the page. The pseudo code looks something like this:
$.ajax({
url: 'first_page.jsp',
type: 'POST',
dataType: 'JSON',
data: {...}
}).done(function(response) {
$.ajax({
url: '2nd_page.jsp',
type: 'POST',
dataType: 'JSON',
data: {...}
});
thisPage.submit();
});
The inner ajax call is not executed if I do not comment out the 'submit' line. I do not understand the behaviour. Can someone please help me with this.
Thanks
You need to execute submit on second ajax sucess.
For example
$.ajax({
url: '',
type: 'GET',
data: {
},
success: function (data) {
$.ajax({
url: '',
type: 'GET',
data: {
},
success: function (data) {
//do your stuff
}
$("input[type='submit']").click(function(event){
event.preventDefault();
$.ajax({
url:'abcd.php',
method:'post',
type:'json',
async:false,
success:function(response){
$.ajax({
url:'abcde.php',
method:'post',
type:'json',
data:{res:response},
success:function(response){
console.log(response);
}
});
$("form").submit();
}
});
});
Try this one.Its working correctly.

JavaScript jQuery AJAX POST data error

I am trying to send a post param. to request.php but it returns that the post param. are empty.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
$.ajax({
url: "request.php",
type: "POST",
data: "{key:'123', action:'getorders'}",
contentType: "multipart/form-data",
complete: alert("complete"),
success: function(data) {
alert(data);
},
error: alert("error")
});
remove " " from this data as data:{key:'123', action:'getorders'}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$.ajax({
url:"request.php",
type:"POST",
data:{key:'123', action:'getorders'},
contentType:"multipart/form-data",
complete:alert("complete"),
success:function(data) {
alert(data);
},
error:alert("error")
});
</script>
You must use FormData for multipart/form-data ,and also need additional option in ajax ..
var request = new FormData();
request.append('key',123);
request.append('action','getorders');
$.ajax({
url: "request.php",
type: "POST",
data: request,
processData : false,
contentType: false,
success: function(data) {
alert(data);
}
});
This will help you. You don't want a string, you really want a JS map of key value pairs.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$.ajax({
url:"request.php",
type:"POST",
data:{key:'123', action:'getorders'},
contentType:"multipart/form-data",
complete:alert("complete"),
success:function(data) {
alert(data);
},
error:function(){
alert("error");
});
</script>
This should work like a champ ,
construct object as below and stringify it as JSON.stringify(newObject) then there will be no chance of error
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
var newObject= new Object();
newObject.key= '123';
newObject.action='getorders'
$.ajax({
url:"request.php",
type:"POST",
data:JSON.stringify(newObject),
contentType:"multipart/form-data",
complete:alert("complete"),
success:function(data) {
alert(data);
},
error:function(){
alert("error");
});
</script>
Try this:
data: JSON.stringify({key: '123', action: 'getorders'}),
contentType: "application/json"

Ajax Json parsed blank result

I am trying to parse some JSON data through AJAX i followed an example from here:
how to parse json data with jquery / javascript?
On the example, they can get it working, but on my exmaple, the turned blank.
I tried just echoing the php, the JSON displayed with no problem either. Wondering what the problem is.
<!DOCTYPE HTML>
<html>
<head>
<link type ="text/css" rel="stylesheet" href= "css/bootstrap.css">
<link type ="text/css" rel="stylesheet" href= "css/account.css">
</head>
<body>
<p id="result">fefe</p>>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script>
$.ajax({
type: 'GET',
url: 'get.php',
data: { get_param: 'value' },
dataType:'json',
success: function (data) {
var names = data
$('#result').html(data);
}
});
</script>
</body>
</html>
What the JSON result looks like in php:
[{"id":"1","userid":"26","title":"654","description":"654"}]
what is the type of data?
try add this line in your code on success function
success: function (data) {
console.log(data);
}
is it an array of objects, if yes maybe you can try this
$.ajax({
type: 'GET',
url: 'get.php',
data: { get_param: 'value' },
dataType:'json',
success: function (data) {
for(var i=0; i<data.length; i++) {
// do your things here using data[i].description until data[i].userid
}
}
});
Try this
$.ajax({
type: "GET",
dataType: "json",
url: "getjobs.php",
data: data,
success: function(data) {
$('#result').html(data);
}
});
Try setting $.ajax's datatype is set to jsonp. Also try to alert the return value.
$.ajax({
type: 'GET',
url: 'get.php',
data: { get_param: 'value' },
dataType:'jsonp',
success: function (data) {
alert('data is ::' +data);
$('#result').html(data);
}
});
Try this:
$.ajax({
type: 'GET',
url: 'get.php',
data: { get_param: 'value' },
dataType:'json',
success: function (data) {
console.log(data);
}
});
in the console(such as chrome browser`s development tools), you can see the actual result.

How can I get the data from an ajax request to appear inside a div?

I am unable to get the data from my ajax request to appear inside <div class="l_p_i_c_w"></div>. What am I doing wrong? I know the function inside my_file.php works, because if I refresh the page, then the data shows up where it should.
jQuery:
$.ajax({
type: "POST",
url: "my_file.php",
dataType: 'html',
success: function(data){
$('div#myID div.l_p_c div.l_p_i_c_w').prepend(data);
}
});
HTML:
<div class="l_p_w" id="myID">
<div class="l_p_c">
<div class="l_p_i_c_w">
<!-- stuff, or may be empty. This is where I want my ajax data placed. -->
</div>
</div>
</div>
CSS:
.l_p_w {
width:740px;
min-height:250px;
margin:0 auto;
position:relative;
margin-bottom:10px;
}
.l_p_c {
position:absolute;
bottom:10px;
right:10px;
width:370px;
top:60px;
}
.l_p_i_c_w {
position:absolute;
left:5px;
top:5px;
bottom:5px;
right:5px;
overflow-x:hidden;
overflow-y:auto;
}
I think if you use prepend you would need to wrap your data object in jquery tags like $(data) before appending, as prepend appends children (objects)
$.ajax({
type: "POST",
url: "my_file.php",
dataType: 'html',
success: function(data){
$('div#myID div.l_p_c div.l_p_i_c_w').prepend($(data));
}
});
However, if you just want to set the html of the div with the data do this:
$.ajax({
type: "POST",
url: "my_file.php",
dataType: 'html',
success: function(data){
$('div#myID div.l_p_c div.l_p_i_c_w').html(data);
}
});
Third Option, try prependTo
http://api.jquery.com/prependTo/
$.ajax({
type: "POST",
url: "my_file.php",
dataType: 'html',
success: function(data){
$(data).prependTo($('div#myID div.l_p_c div.l_p_i_c_w'));
}
});
One last attempt:
$.ajax({
type: "POST",
url: "my_file.php",
dataType: 'html',
success: function(data){
$('div#myID div.l_p_c div.l_p_i_c_w').html(data + $('div#myID div.l_p_c div.l_p_i_c_w').html());
}
});
$('div#myID div.l_p_c div.l_p_i_c_w').prepend(data);
should be
$('#myID .l_p_c .l_p_i_c_w').html(data);
What about prependTo() ?
$.ajax({
type: "POST",
url: "my_file.php",
dataType: 'html',
success: function(data){
$(data).prependTo('#myID .l_p_c .l_p_i_c_w');
}
});
As #rajesh mentioned in his comment to your question, try alerting the data in the success to make sure it's coming back as expected:
$.ajax({
type: "POST",
url: "my_file.php",
dataType: 'html',
success: function(data){
alert(data);
}
});
Try this:
$.ajax({
type: "POST",
url: "my_file.php",
dataType: 'html',
complete: function(jqXHR, settings){
if (jqXHR.status == 200)
$('div#myID div.l_p_c div.l_p_i_c_w').prepend(jqXHR.responseText);
}
});
or
$.ajax({
type: "POST",
url: "my_file.php",
dataType: 'html'
}).done(function(data) {
$('div#myID div.l_p_c div.l_p_i_c_w').prepend(data);
});

Categories