如何制作jsonP

时间:2014-04-14 16:34:39

标签: php jquery ajax jsonp

我有一个ajax代码如下

xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status==200) {
token=xhr.responseText;
//add value/field to form
txCvv = txJ$(".cvv").val();
var MyForm = txJ$(".submit").closest("form");
if (txJ$('#cipherText').length > 0) {
    txJ$('#cipherText').val(txCryptData);
}
else
{
    txJ$('<input type="hidden">').attr({
            id: 'token',
            name: 'token'
        }).val(token).appendTo(MyForm);
    txJ$('<input type="hidden">').attr({
            id: 'cvv',
            name: 'cvv'
        }).val(txCvv).appendTo(MyForm);



}

//scrub data that we do not want to post
txJ$(".data").removeAttr('name');
txJ$(".cvv").removeAttr('name');
    }
}
xhr.open('POST', 'tokenize.php', true);
xhr.send(params);

tokenize.php驻留在我的服务器上,ajax工作正常。这个javascript也驻留在我的服务器上。当从不同的服务器,我在我的服务器上添加了javascript源,但它显示我的错误,它试过从该服务器访问tokenize.php。

tokenize.php是

<?php
     $encrypted_data = file_get_contents("php://input");
    //There are some codes here.
    echo $output;
<?

我可以在我的服务器中使用javascript并轻松地从php获取数据到脚本。但是如果我想为我的客户端使用javascript,或者在不同服务器上使用任何人,他们将如何获取数据来自我服务器上的php文件?

我在网上看过jsonP,其中可以从远程服务器发出请求。在我的代码中,是否可以实现jsonP,以便我的客户端可以从我服务器上的tokenize.php访问数据,如果是的话我应该做什么修改。我对jquery很新,所以很难搞清楚,请帮忙

0 个答案:

没有答案