0

I have a ajax code as follows

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);

The tokenize.php resides in my server and the ajax is working fine.This javascript also resides on my server.When from a different server,I added the javascript source on my server,but it showed me error,that it tried to access tokenize.php from that server.

The tokenize.php is

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

I can use the javascript in my server and get a data from php to the script easily.But if I want to use the javascript for my clients,or any one who is on a different server,how would they get the data from php file on my server?

I have seen online about jsonP ,in which request can be made from remote server.In my codes,is it possible to implement jsonP ,so that my clients can access datas from tokenize.php which is on my server,if yes,what modification should I do.Im really new to jquery,so finding it hard to figure it out,please help

Piya
  • 1,134
  • 4
  • 22
  • 42
  • Since you have full control over the PHP script, I recommend to use CORS. Implementing CORS is much easier than JSONP. – Rob W Apr 14 '14 at 16:37
  • thanks for the response.CORS?I havnt heard of it.will check it.Any modification shall be done in this code? – Piya Apr 14 '14 at 16:38
  • Only at the server's side. – Rob W Apr 14 '14 at 16:39
  • Ya..I checked this here http://stackoverflow.com/questions/17318426/cors-cross-domain-ajax-without-jsonp-by-allowing-origin-on-server Can you please help me out on ,waht changes will I have to do in my php..Can you give me a solution if you dont mind? – Piya Apr 14 '14 at 16:41
  • Put `header('Access-Control-Allow-Origin: *');` on the line after ` – Rob W Apr 14 '14 at 16:43
  • Ok Thanks @RobW this means that it allows anyone to access the php right? – Piya Apr 14 '14 at 16:45
  • It means that any website can call your API and read its response... – Rob W Apr 14 '14 at 16:45

0 Answers0