Ajax请求使用javascript外部URL [英] Ajax request to external url using javascript

查看:333
本文介绍了Ajax请求使用javascript外部URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着做一个Ajax请求外部url.Right现在即时通讯做它在PHP

Im trying to do an ajax request to an external url.Right now im doing it in php

$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.com/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);      

我试着使用javascript.I尝试过这种使用jQuery做同样的,但没有制定出

Im trying to do the same using javascript.I tried this using jquery,but didnt work out

code jQuery的,我试过是

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.com/rest/',
    data: {data:$json},
    success: success,
    dataType: "json"
});
alert(result);
function success(result) { 
    alert('done');
}

林新在客户端端浏览器scripts.Kindly帮我尝试了上面的JavaScript,我prefer.Looking着一些帮助。

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.

推荐答案

如果服务器不支持CORS,或不使用JSONP响应,你需要使用一个中间人脚本来获取数据。

If 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.com/myPhpScript.php',
    success: success,
    dataType: "json"
});

PHP

...
$outputjson = curl_exec($ch);
echo $outputjson;
die;

如果他们使用JSONP响应,你可以使用jQuery的JSONP的能力来获取数据,只看如何调用自己的休息服务,他们应该这样做一个JSONP调用如果他们这样做允许它的细节其他域的文档。一般的code会是这样的:

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.com/rest/?callback=?',
    data: {data:$data},
    success: success,
    dataType: "jsonp"
});

不过,你有什么看起来像一个API密钥和TokenID,如果这些都应该是保密的(即最终用户不应该能够看到他们),那么你不应该使用,显示这些细节的JavaScript,即使用中间人脚本。

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.

这篇关于Ajax请求使用javascript外部URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆