parse HTML table from third party server (jQuery or PHP) - php

I have a HTML table with sport results on a third party server that I would like to parse as a JSON or XML so I can grab me the values out of it...
I would prefer to do this with jQuery and already played around with $.ajax but I don't get it running :/
I also thought about a PHP script running on my server and doing something with file_get_contents() and parsing the result as JSON - without success...
Dose anyone have an idea - what is the best solution to do what I want? I need a thought-provoking impulse ;)
My jQuery attempt:
$.ajax({
dataType: "jsonp",
url: "....",
success: function(data) {
console.log(data);
},
error: function() {
console.log('error');
}
});
Running in to an error:

As per comments:
For cross-site content, make a proxy script using PHP. All this will need to do is grab the remote content and echo it out.
Your ajax request will then point to this script instead, and you will be able to parse the response using standard jQuery functions as if it were a normal page e.g. $(data).filter('table');

Your 'dataType' is wrong? Shouldn't that be either 'html' or 'json' since i see you are receiving HTML content.

Related

JSON: Retrieving JSON file from Github

After awhile of not using JSON, I'm a little rusty on the possibility of requesting the data from another domain/web-server.
I'm just trying to get the JSON resource to load onto my web server when the JSON file is hosted from GitHub. Using AnyOrigin, I used this script
$( document ).ready(function() {
$.ajax({
dataType: "json",
url: 'http://anyorigin.com/get?url=,https%3A//raw.githubusercontent.com/testuser/testbin/master/data/&callback=?',
success: function(result){
run(result);
}
});
});
In my index.php, I have a file that references the json.. but it doesn't parse it.
<a href="https://raw.githubusercontent.com/testuser/testbin/master/data/<?php echo $post['filename']; ?>">
I am a bit lost at this point. What am I doing wrong? Am I not specifying something correctly? All help is appreciated. Thank you for your time.
You need to use JSONP as a dataType in your ajax request as you are sending a request to the external link. Also, need to need to receive the response in the callback function with JSON.
For more detail, check https://www.w3schools.com/js/js_json_jsonp.asp
This example might be helpful for you as well https://www.sitepoint.com/jsonp-examples/

Parsing RSS with php to get a json feed

I need to convert a RSS feed from another domain and turn it into json. In order to circumvent cross-domain warnings, I'm parsing the RSS feed server side using this PHP proxy script.
but somehow the response is in one long string.
I would like to use ajax like this:
var url = "http://www.mywebsite.net/simple-proxy.php?url=http://feeds.bbci.co.uk/news/rss.xml?edition=int&callback=feed";
$.ajax({
dataType: "jsonp",
url: url,
success: function(data) {
console.log(data);
}
});
- but somehow the response is in one long string. How do I get the response turned into a json array with nodes, and so on?
Get the string and then convert it into JSON:
I see you are using jquery so...
$.parseJSON(jsonString);
I found a solution using SQL instead.

simple jsonp not working with php server

I tried following some basic examples, and it is not working. I am not sure I completely understand jsonp, but I followed the basic tutorials and on an intuitive level, I can't see anything wrong. (Of course, I am missing something, hence the question).
JavaScript code:
postData = $(this).serialize();
console.log(postData);
$.ajax({
data: postData,
url: 'externaldomain.php',
dataType: 'jsonp',
success: function(data){
console.log(data);
alert('Your comment was successfully added');
},
error: function(){
console.log(data);
alert('There was an error adding your comment');
}
});
PHP code:
$tag = mysql_real_escape_string($_GET["callback"]);
The annoying part is that it is not even showing me an error to Google for.
Can anyone help out figuring the problem please?
Since you haven't posted your full or relevant PHP code I'm going to assume it looks like this
$tag = mysql_real_escape_string($_GET["callback"]);
echo $tag."(";
// print json here
echo ")"
I'm not really sure how jquery handles jsonp requests but what I used to do is add a new script tag to the DOM that looks like this
<script> function cb(json) { alert(json); } </script> // (I)
<script src="{url}?callback=cb"></script> // (II)
When (II) is loaded, and because we have a callback then the script that we are including in the DOM looks like this
cb({
// json goes here
})
which pretty much is like a function call to some function called cb that we are using. It is therefore natural to have an implementation of cb (which is in (I)). That implementation is similar to the success(data) function in jQuery's ajax. It is used to use and manipulate the json requested
However I noticed that you are doing a POST request along the ajax call with jsonp. Unfortuantely (according to this question How to make a jsonp POST request that specifies contentType with jQuery?) doing POST with JSONP is not feasable due to implementation pursposes.
You can't POST using JSONP... it simply doesn't work that way...
So you can usually only perform GET requests. You can NOT perform POST requests.
For details about how jsonp work look at this nice article. jsonp-how-does-it-work
For more clearification :
See Other SO question Link1 Link 2
But there is work around for that with some limitations look at this Using PUT/POST/DELETE with JSONP and jQuery. (for me nothing works at all)

Ajax response receiving as XML

I'm getting all ajax responses as XML only. When i implement them, it worked fine(i got the responses as HTML). Is there any reason that we are receiving the responses as XML by default? I think something has been changed recently. But i couldn't able to corner the change. Any help on this will be greatly appreciated.
NOTE: Please note that I'm using jQuery for AJAX.
Here is the code i'm using in one of the place in my site (in all the places i'm using in the same manner and it is coming in XML only, as shown in the screenshot)
$.ajax({
type: "POST",
url: "/ajax_contests_submissions_more&popup=yes",
dataType:"html",
data: 'last_pos='+queryPos,
cache: false,
error:function(XMLHttpRequest, textStatus, errorThrown) {
alert('sorry, we were unable to process your request. please try again later');
},
success: function(html)
{
}
});
Please check this image which shows that browser will not guess the XHTML as XML as per #dystroy's comment.
AJAX replies tries to parse the response as XML. So you can grab either XML or the raw text.
See this

jquery message system without php how to?

i took some interest in this script http://www.9lessons.info/2009/06/comment-system-with-jquery-ajax-and-php.html
and i see that the ajax calls commentajax.php.
what i want to do is to ignore that php, because i want to post to a json file and then get the response from the same file.
my server will use POST or PUT to put the data in the database, so there is no need for me to use php, just the syntax is killing me :)
i want to use :
$.ajax({
type: "POST",
url: "http://www.xxx.com/json",
data: dataString,
cache: false,
success: function(html){
$("ol#update").append(html);
$("ol#update li:last").fadeIn("slow");
document.getElementById('comment').value='';
$("#name").focus();
$("#flash").hide();
}
});
but then how would the commentajax.php look like?
maybe replace the php with :
$.getJSON('http://www.xxx.com/json' , function(data) { ... });
any idea helps
Thanks.
edit1: i have the server-side script in place
If you have the server side scripting set up already, then what is the question again?
If you're asking how to handle the ajax call, then it's mostly a matter of looping through the JSON that you get back, and applying those values to the site in some manner. Pseudo code:
$.getJSON('http://www.xxx.com/json' , function(data) {
for(i=0; i<data.comment.length; i++) {
$(".commentTitle").html(data.comment[i].title);
$(".commentBody").html(data.comment[i].text);
}
});
If I am reading this correctly:
because i want to post to a json file and then get the response from the same file.
You are going to need some server side scripting in order to 'post' to the json file. How are you getting the data into the file.
You can 'read' the data file from the server, that's not a problem, it is a matter of getting the data into the file that you need the server side scripting for.

Categories