0

I'm having trouble retrieving JSON data from an AJAX call.

I tried to get JSON data from different webpages with PHP cURL; this works successfully. The PHP page is called from a Jquery AJAX call. I can get the JSON data to return as a string from one of the websites, but not the other.

jQuery

    var formData = {"data1" : a, "data2" : b, "data3" : c};

    $.ajax({
        dataType: "json",
        contentType : "application/json; charset=UTF-8",
        url: '/getjson.php',
        data : formData,
        success: function(response) {
            CurrentArray = response;
        }
    });

PHP

<?php
//select webpage to get JSON from
switch ($_GET['data1']){
    Case 1: 
        $result = get_with_curl($webpage1);//select webpage 1
    break;

    Case 2: 
        $result = get_with_curl($webpage2);//select webpage 1
    break;
}
echo $result['FILE'];
?>

1. What works

  • AJAX call to PHP which is executing a CURL call
  • Curls sends back a JSON string with Content Type : text/html; charset:UTF-8
  • This JSON string is retrieved back in the AJAX call

The symbol (in the # column) of this command is displayed as <> in fiddler

2. What doesn't work

  • When doing this at another site, CURL sends back a JSON string but the Content-Type of the retrieving curl command in PHP is now applcation/json; charset;UTF-8. When I echo this in PHP, the object will not retreived in AJAX.

Here the symbol (in the # column) of this command is displayed as {JSON} in the fiddler.

When I modify Data Type into "Text" and the Content Type into "text/plain ; charset:UTF-8", The data (as from point 2) is retreived in text; but I want the data in an object format.

Relevant AJAX PHP issue 1

Relevant AJAX PHP issue 2

Sinister Beard
  • 3,570
  • 12
  • 59
  • 95
  • Add your php code please. – fiction Jan 05 '15 at 12:56
  • You seem to have three different scripts (a JavaScript one, a PHP one that uses CURL to make HTTP requests and another PHP one that responds to the first PHP script — I can't tell if the JS is talking to the first or second PHP script) – Quentin Jan 05 '15 at 12:58
  • 2
    Your JavaScript doesn't make a whole lot of sense. You are trying to set a Content-Type request header, but you are making a GET request (so there is no content to describe the type of) and you aren't formatting the data as JSON. – Quentin Jan 05 '15 at 12:59
  • add type: "POST", In php use $_POST – Dilip Godhani Jan 05 '15 at 13:03
  • 1
    @DilipGodhani — `$_POST` won't be populated for a JSON request. – Quentin Jan 05 '15 at 13:25
  • in your PHP page add in this header: header('Content-type: application/json'); – PHPology Jan 05 '15 at 13:31
  • PHP code is added. @Quintin, only one PHP is requested from AJAX. In the PHP script a selection wil be made which site will be requested for data – user3745930 Jan 05 '15 at 13:36
  • I've removed the Content Type from the AJAX call and added header('Content-type: application/json'); at the fiest line of the PHP code. but still have the same result – user3745930 Jan 05 '15 at 13:45
  • This is an example of the result that is comming from cURL. [{"type":"catagory","items":[{"id":"1","name":"one","googleAnalyticsUrl":"/SYI/SELECTCATEGORY/1_one"},{"id":"31","name":"two","googleAnalyticsUrl":"/SYI/SELECTCATEGORY/two"}]}] When I save this string to an file and load it later (without calling the curl function), then this string is read by the AJAX call. In other words: The string will only read when hardcoded and the curl function is not called. – user3745930 Jan 05 '15 at 13:53

1 Answers1

0

Finally Solved the problem, I completely teared down the PHP cUrl function, in a way that only basic functionality was left. Now the JSON data is returned to the AJAX call. What I noticed was that there were some warnings when the cUrl function was executed (like undefined variables) but the JSON data was still returned in the PHP function. So, primary you should think that warnings should be no problem. After resolve the warning coming from the cUrl function, the JSON data as returned properly to the AJAX call. In other words, the Ajax call will not return data when a PHP warning is occurring.