Im using a mixture of php and javascript/jquery to scrape a websites prices from there webpage, there's no API so unfortunately I scrape the html page and pick up the prices/title from the html(I know this is a really risky way of doing it but its the only way).
Anyway, this is whats going on:
I pull in the external webpage using php file_get_contents()
This method is within a foreach loop, for every external page I want to grab data from.
This is my javascript.
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js' type='text/javascript'></script>
<script type='text/javascript'>
console.log('page-{$i}');
$('.page-{$i}').find('.search_result_row').each(function(i, obj) {
//This will give us each individual apps details.
var appTitle = $(this).find('.title').text();
var appPrice = $(this).find('.search_price').text();
var appDiscount = $(this).find('.search_discount').text();
var appDetails = {
'appTitle' : appTitle,
'appPrice' : appPrice,
};
callAjax(appDetails);
var my_delay = 5000;
function callAjax(appDetails) {
$.ajax({
url: 'Upload.php',
type: 'POST',
data: appDetails,
dataType: 'JSON',
success:function(data) {
console.log(data);
},
error:function(jqXHR, textStatus, errorThrown) {
console.log('request failed ' + textStatus + errorThrown);
console.log(appDetails);
}
});
}
});
</script>
Everything works fine for the first URL, and for about half of the urls that I'm grabbing data from. The issue is SOME of the data being sent via ajax is returning the following error
Upload.php net::ERR_EMPTY_RESPONSE
Can any of you help?
Try this. You must stringify your details before you post them as json. This could be a reason for ERR_EMPTY_RESPONSE.
function callAjax(appDetails) {
appDetailsJsonString = JSON.stringify(appDetails);
$.ajax({
url: 'Upload.php',
type: 'POST',
data: {appDetailsJson: appDetailsJsonString},
dataType: 'json',
success:function(data) {
console.log(data);
},
error:function(jqXHR, textStatus, errorThrown) {
console.log('request failed ' + textStatus + errorThrown);
console.log(appDetails);
}
});
}
Related
Hi I am currently learning php and I am trying to get data from php file using the below script but i am not getting any response
$.ajax({
type: 'POST',
url: "mark_mod.php",
data: data_set,
dataType: "JSON",
success: function(data) {
alert("Response : " ); // not triggering
}
});
my php return stmt
There might be problems with File URL or file access. You can use complete callback to check request for errors like that:
$.ajax({
type: 'POST',
url: "mark_mod.php",
data: data_set,
dataType: "JSON",
success: function(data) {
alert("Response : " );
},
// This method will be fired when request completes
complete: function(xxhr, status) {
alert("Status code: " + status);
}
});
If the status code is not success that means there is something wrong with your request.
You can check more callback options here.
It doesn't matter whether you use return or echo in your PHP file.
The success method must get a call if it's fine. However, if you use
return instead of echo, nothing will append to your request's data.
On the other hand, The PHP response will include in your 'data' variable in the function success.
You need use data in the assync function success
success: function(data) {
alert("Response : " + data);
},
Thanks for your Responses. I got the Solution to my problem. It seems since Ajax is asynchronous... its taking time to receive the resultant echo value from my php file. so I had to synchronize my Jquery using async : False.
$(function(){
$('#formID').on('submit',function(){
const data_set={
name:"Nipu chakraborty",
"phone":"01783837xxx"
}
$.ajax({
type: 'POST',
url: "mark_mod.php",
data: data_set,
dataType: "JSON",
success: function(data) {
alert(data);
}
});
});
});
The Flickr API defaults to search for the most recent photos. I want my API to sort by most relevant photos first. I tried adding "&sort=relevance" to my url and while the API still returns it is not sorting by relevance.
Here is my base code:
function getFlickr(query) {
var proxy = 'https://cors-anywhere.herokuapp.com/';
var url = 'https://api.flickr.com/services/feeds/photos_public.gne?tags=' + query + '&sort=relevance&format=json&nojsoncallback=1';
$.ajax({
type: "GET",
url: proxy + url,
// dataType: "json",
success: function(response) {
renderFlickrResults(response, query);
},
error: function(xhr, status, e) {
console.log(status, e);
}
});
}
What am I missing? Thank you for your help!
I have a JSON file which i want to load end it is working fine when I am trying to load that file locally from my hard drive.
function save(){
$.ajax({
type: "GET",
url: 'cameras.json',
dataType: "JSON",
async : false,
success: function(data) {
var output = data.result["0"].Address;
alert(output);
},
error: function(data) {
alert("Error")
}
});
}
When i want to get access to this JSON file from my server:
function save(){
$.ajax({
type: "GET",
url: 'http://192.168.0.21:8080/json.html?type=cameras',
dataType: "JSON",
async : false,
success: function(data) {
var output = data.result["0"].Address;
alert(output);
},
error: function(data) {
alert("Error")
}
});
}
It's not working and I am getting error script.
What can be issue with that ?
I had the same problem.
Unfortunately, I think that remote (i.e. away from localhost) ajax calls are forbidden - or something similar - for security reasons.
Depending on your purposes, you can resolve the problem with a 2-steps action:
You can call - via ajax - a local server class which loads the remote file via REST web service.I solved this way...
I am just trying to read the file content of a rendering HTML of URL
Here the code i am using , its always going in error section .
$.ajax({
type: 'POST',
url: 'http://www.withholding32.com/api/wh32calc.php?userid=nick&fpp=12&ffs=Single&fa=0&fgp=6000&figp=0&fiytd=0&st=6&stp=6000&ss=Single&sa=0&sad=0&stca=0',
dataType: 'html',
success: function(data) {
alert('success');
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert('error');
}
});
but in case i run the same url directly in browser , its show me html.
here is url
Working DEMO
You can use this in your head tag
<script src="https://rawgithub.com/IonicaBizau/jQuery-cross-domain-requests/master/js/jquery.xdomainajax.js">
</script>
code
$.ajax({
url: 'http://www.withholding32.com/api/wh32calc.php?userid=nick&fpp=12&ffs=Single&fa=0&fgp=6000&figp=0&fiytd=0&st=6&stp=6000&ss=Single&sa=0&sad=0&stca=0', // Or your web page link
type: 'GET',
success: function(res) {
var headline = res.responseText;
$('body').append(headline);
}
});
Hope this helps, Thank you
Try the below code:
$('document').ready(function() {
$.getJSON('http://anyorigin.com/get?url=' +
encodeURIComponent('http://www.withholding32.com/api/wh32calc.php?userid=nick&fpp=12&ffs=Single&fa=0&fgp=6000&figp=0&fiytd=0&st=6&stp=6000&ss=Single&sa=0&sad=0&stca=0') + '&callback=?',
function(data){
$("#result").html(data.contents);
});
});
Refer : http://jsfiddle.net/R7EPt/275/
Change your request type to GET, all your parameters are given in the URL.
if you are using post method for the ajax than you can't pass argument with url and also add control origin to your php file.
try this...
AJAX code:
$.ajax({
type: 'POST',
url: 'http://www.withholding32.com/api/wh32calc.php',
dataType: 'html',
async:false,
data: 'userid=nick&fpp=12&ffs=Single&fa=0&fgp=6000&figp=0&fiytd=0&st=6&stp=6000&ss=Single&sa=0&sad=0&stca=0',
success: function(data) {
alert('success');
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert('error');
}
});
PHP CODE:
header("Access-Control-Allow-Origin: *");
I'm running an application using Ajax and Sinatra. I want to send a POST parameter into my app.rb file.
My app.rb
post '/game/moves' do
#square = params[:square]
puts #square
content_type :json
{ :success => 'Data successfully transmitted' }.to_json
end
My view
$.ajax({
url: 'moves',
data: {square:square},
type: 'POST',
dataType: 'json',
success: function() {
alert("Success");
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest + textStatus + errorThrown);
}
});
It returns the alert "[object Object]error", but on my console, I see that I successfully retrieved the POST parameter. So it works, but I can't get into the "success" part of the code, and instead I'm getting on the error part.
I also tried to replace the json type by the html type, and removed the return value on my app.rb file, to no avail (exact same error).
I found a working solution. Here, I successfully retrieve the params on top ofgetting the "It works !" alert. That was what I wanted.
As you can notice, I don't set "async" to false. I don't know why I have to do that, nor why it doesn't work when I don't. I don't mind if it is set to false, for it works, but if someone can tell me the possible reason, it would still help me. Thanks.
Here's the full code (the function send is called in an obstrusive way (with a one-line html onclick), because I need the params xi and yi)
The view
function send(xi,yi) {
info = [];
info[0] = xi;
info[1] = yi;
$.ajax({
type: 'POST',
url: 'moves',
dataType: 'text',
async: false,
data: {square:info},
success: function(token) {
alert(token)
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest, textStatus, errorThrown);
}
});
}
App.rb
post '/game/moves' do
if params[:square]
"It works !"
end
end