0

I have a problem when i try to get data from a php script, I get this error

Uncaught SyntaxError: Unexpected token <

When I open once the link to php script, my ajax request works but when I delete browser's cache the problem heppens again!

This is my JavaScript code:

$.ajax({ 
    url : "http://xxxxx/xxxx/xxxx.php?callback=test",
    type : "GET",
    dataType : "jsonp",
    jsonpCallback: "test",
    contentType: "application/javascript",
    async: true,
    success : function(data) {
        console.log("success");
    },
    error : function(request, status, error) {
        console.log(error);
    }
});

This is the PHP code:

<?php

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept,Authorization, X-Request-With');
header('Access-Control-Allow-Credentials: true');

test_function();
function test_function(){
    $return = array("reponse"=>"false");
    try {
        include_once $_SERVER['DOCUMENT_ROOT'].'/bd/db_connect.php';
        $db = new DB_CONNECT();
        $con = $db->connect();
        $sql = "SELECT * FROM `xxx` ";
        $result = $con->query($sql);
        if ($result->num_rows > 0) {
            $response ['reponse']="true";
            $response["data"] = array();
            while ($row = $result->fetch_assoc()) {

                $rest = array();
                $rest["xxx"] = $row["xxx"];
                $rest["xxx"] = $row["xxx"];
                array_push($response["data"], $rest);
            }

        } else {
            $response ['reponse']="false";
        }
    } catch (Exception $e) {
        $response ['reponse']="false";
    }
    echo $_GET['callback'] ."(".json_encode($response).")";
    //echo "(".json_encode($response).")";
}?>

I get this in the response. Link to image

Wissem Achour
  • 15
  • 1
  • 5
  • I guess the other site requires some kind of cookies set. If they are not set, you'll get a http error page, which in your JS leads to the seen error – Sirko Jan 01 '16 at 11:04
  • The problem is with the PHP script, not the javascript. Can you show the code for that? – rjdown Jan 01 '16 at 11:09
  • It sounds like the PHP script doesn't really implement JSONP. It should be sending back Javascript that looks like `test({...});`. – Barmar Jan 01 '16 at 11:12
  • 1
    `contentType: "application/javascript"` is wrong. That header is for the type of data that you're **sending** in the post data. Since you're using `GET`, not `POST`, there is no content. – Barmar Jan 01 '16 at 11:21
  • In this line you add some raw text to you json string wich make it's format different and the out put is not json anymore : echo $_GET['callback'] ."(".json_encode($response).")"; – Mostafa -T Jan 01 '16 at 12:05
  • I tried all but still not working , anyway I get html as a response and for me it is strange because I expect json as a response – Wissem Achour Jan 01 '16 at 13:11
  • I added a picture of ajax response on the bottom of the question. – Wissem Achour Jan 01 '16 at 13:16
  • If you add this to the top of your PHP file does it work? `header('Content-Type: application/json');` ? – Clay Jan 01 '16 at 13:20
  • And... are you using any frameworks or anything? – Clay Jan 01 '16 at 13:22
  • No still not working, I tried it before :) – Wissem Achour Jan 01 '16 at 13:23
  • I dont use any framework. – Wissem Achour Jan 01 '16 at 13:24
  • See http://stackoverflow.com/questions/34516880/javascript-errors-reference-what-does-this-error-mean/34516918#34516918 – Michał Perłakowski Jan 02 '16 at 09:49
  • Nothing worked , just worked when i opened the php link in the browser after that all ajax requests worked! but once i clear the cache i have the same error! it could be the problem of the free host domain? – Wissem Achour Jan 03 '16 at 08:45

2 Answers2

0

You must check browser url domain prefix http://www or http:// and this should match with ajax url.

0

If you're making a JSONP call, response must be valid JavaScript code, not HTML.

See also: Use JSONP to load an html page

Community
  • 1
  • 1
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177