Im trying to do an ajax request to an external url.Right now im doing it in php
as
$data = array(
'TokenID' => $tokenid,
'APIKey' => $api_key,
'EcryptedData' => $encrypted_data,
'TokenScheme' => 4
);
//convert to JSON
$json = json_encode($data);
//curl config
$ch = curl_init("/");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json', //we are using json in this example, you could use xml as well
'Content-Length: '.strlen($json),
'Accept: application/json') //we are using json in this example, you could use xml as well
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$outputjson = curl_exec($ch);
Im trying to do the same using javascript.I tried this using jquery,but didnt work out
code for jquery which I tried is
$data = array(
'TokenID' => $tokenid,
'APIKey' => $api_key,
'EcryptedData' => $encrypted_data,
'TokenScheme' => 4);
$json = json_encode($data);
$.ajax({
type: "POST",
url: '/',
data: {data:$json},
success: success,
dataType: "json"
});
alert(result);
function success(result) {
alert('done');
}
Im new to both the client side browser scripts.Kindly help me try the above to javascript,which I prefer.Looking forward for some help.
Im trying to do an ajax request to an external url.Right now im doing it in php
as
$data = array(
'TokenID' => $tokenid,
'APIKey' => $api_key,
'EcryptedData' => $encrypted_data,
'TokenScheme' => 4
);
//convert to JSON
$json = json_encode($data);
//curl config
$ch = curl_init("https://testingonetwo./rest/");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json', //we are using json in this example, you could use xml as well
'Content-Length: '.strlen($json),
'Accept: application/json') //we are using json in this example, you could use xml as well
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$outputjson = curl_exec($ch);
Im trying to do the same using javascript.I tried this using jquery,but didnt work out
code for jquery which I tried is
$data = array(
'TokenID' => $tokenid,
'APIKey' => $api_key,
'EcryptedData' => $encrypted_data,
'TokenScheme' => 4);
$json = json_encode($data);
$.ajax({
type: "POST",
url: 'https://testingonetwo./rest/',
data: {data:$json},
success: success,
dataType: "json"
});
alert(result);
function success(result) {
alert('done');
}
Im new to both the client side browser scripts.Kindly help me try the above to javascript,which I prefer.Looking forward for some help.
Share Improve this question edited Mar 29, 2014 at 12:12 Piya asked Mar 29, 2014 at 12:07 PiyaPiya 1,1444 gold badges22 silver badges44 bronze badges 6- You are json-encoding to $json but sending $data – Niklas Commented Mar 29, 2014 at 12:11
- 1 you cannot access other domains through javascript, you will get a cross domain access error. Unless the other domain uses cors or they employ jsonp responses you will not be able to get the data. – Patrick Evans Commented Mar 29, 2014 at 12:12
- @PatrickEvans Is there anyway I could do it?Some work arounds? – Piya Commented Mar 29, 2014 at 12:12
- @niklas Sorry that was a typoo – Piya Commented Mar 29, 2014 at 12:13
- Use a middleman, ie call your php script above let it get the json data and simply echo it out, and instead of pointing the ajax call to the other domain point the url to that php script – Patrick Evans Commented Mar 29, 2014 at 12:14
1 Answer
Reset to default 5If the server does not support cors, or does not employ JSONP responses you will need to use a middleman script to get the data.
JS
$.ajax({
type: "POST",
url: 'https://YourOwnDomain./myPhpScript.php',
success: success,
dataType: "json"
});
PHP
...
$outputjson = curl_exec($ch);
echo $outputjson;
die;
If they employ jsonp responses you can use jQuery's jsonp abilities to get the data, look at the other domain's documentation on how to call their rest service they should have details about doing a jsonp call if they do allow it. The general code would be something like:
$data = {
'TokenID':$tokenid,
'APIKey':$api_key,
'EcryptedData':$encrypted_data,
'TokenScheme':4};
$.ajax({
type: "POST",
url: 'https://testingonetwo./rest/?callback=?',
data: {data:$data},
success: success,
dataType: "jsonp"
});
But as you have what looks like a API key and a TokenID if these are supposed to be secret (ie the end users should not be able to see them) then you shouldnt use the javascript that shows those details, ie use the middleman script.