2

I have this JSON data:

$.ajax({
    type: "GET",
    url: "http://www.example.com/test.php",
    data:"code=Sh9QA&token=0982ff3066a3c60dbd3ecf9bcafc801b",
    contentType: "application/json; charset=utf-8",
});

To send this data to http://www.example.com/test.php, I have tried with this code:

<?php

//API URL
$url = 'http://www.example.com/test.php';

//Initiate cURL.
$ch = curl_init($url);

//The JSON data.
$jsonData = array(
    'data' => 'code=Sh9QA&token=0982ff3066a3c60dbd3ecf9bcafc801b'
);

//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);

//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

//Execute the request
$result = curl_exec($ch);

?>

But, it always retuns No access.

What are wrong in my code? Can you help me to fix it?


Sorry about my English, it is not good. If my my question is not clear, please comment below this question.

2 Answers2

0

Without the documentation to look out the only thing I can suggest is to remove the data from the array and just make it key code.

<?php

//API URL
$url = 'http://www.example.com/test.php';
$data = "?code=Sh9QA&token=0982ff3066a3c60dbd3ecf9bcafc801b"

//Initiate cURL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//Execute the request
$result = curl_exec($ch);

?>
Ethan22
  • 747
  • 7
  • 25
  • Ah, I kinda understand more. If it's a GET request you don't need half of those options. You just need to append the data onto the end of the url – Ethan22 Jul 31 '15 at 03:13
0

First check http://www.example.com/test.php

Ajax system can't be used with full domain name.

so you should use /test.php

Then checking for an error that occurs in your site or target site.

Then the code becomes:

$.ajax({
    type: "GET",
    url: "/test.php",
    data:"code=Sh9QA&token=0982ff3066a3c60dbd3ecf9bcafc801b",
    contentType: "application/json; charset=utf-8",
    success: function(data, textStatus) {   
        alert(data);
        data = $.parseJSON(data);
    },
    error : function(data, textStatus, error){                  
        alert(data + "  : "+ textStatus);
    }
});
hoss
  • 2,430
  • 1
  • 27
  • 42
bamtoggi
  • 30
  • 2