0

i know PHP is a server side script and js is a client side but i have an issue..

I needed to bypass the browser security when doing an ajax request to another domain and it was difficult for me to be honest i had no idea what i was doing so i turned to php.. wooohooo....

The issue is i can only use JS and CSS to run arbitrarily ontop of my tracking system!

I wanted to add a newsfeed.csv so i made this php code to get the csv and convert it and store it into the variable $json.

    <?php
$file="http://www.jonar.com/portal/partner/js/newsroomcustomer.csv";
$csv= file_get_contents($file);
$checkit = utf8_encode($csv);
$array = array_map("str_getcsv", explode("\n", $checkit));
$json = json_encode($array);
?>

Is there a way to do an ajax get to get the variables output? meaning the json format of my csv..

This way i can have my PHP script on my webserver and it will be able to grab any link on any domain..

Thanks guys :)

9
  • 2
    You only missed the one bit to output yout data ... echo $json; Commented Oct 3, 2015 at 15:41
  • this is common practice....you are using your server as proxy for ajax request. Don't forget to add some error handling Commented Oct 3, 2015 at 15:56
  • 1
    make an ajax request to path on server that runs that code Commented Oct 3, 2015 at 16:19
  • 1
    did you add the echo? Also will have to parse array returned to html in your ajax caallback Commented Oct 3, 2015 at 16:23
  • 1
    are you calling a server? Can't run php without it being on server Commented Oct 3, 2015 at 16:38

1 Answer 1

1

Like the comment of Julio said:

Use echo to print the contents of the generated JSON string. It's as easy as echo $json.


Once your PHP script outputs the correct JSON string, you can send an AJAX request to your script with the following code (assuming you're using jQuery, which will make your life easier):

$.get('script.php', function (data) {
   console.log('Data received: ', data);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Yes i know that will print my output but how can you implement the output of a file.php inside an ajax request? or any type or request
here's the issue i was having even with echo $json; when i use your script or even an ajax call it returns the contents of the PHP file.. not the json string.. it gives me the whole php function @hidde

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.