0

I have the following ajax ,on which im trying to post the dataString as a parameter to the php file. I have tried putting dataString inside xhr.send(dataString);.But it didnt work out.Is there any way around?

dataString = txCryptData;
var xhr = new XMLHttpRequest();
var params="data="+dataString;
xhr.onreadystatechange = function() {
 if (xhr.readyState == 4) {
       alert(xhr.responseText);
     }
}
xhr.open('POST', 'tokenize.php', true);
xhr.send();  

In the php I tried $_POST['params']; to fetch the value posted by the ajax req

2
  • Why don't you send variable params? Commented Mar 30, 2014 at 15:47
  • Sorry I tried it.But didnt succeed.I think the problem is in the php? Im using $_POST['params']; there to fetch Commented Mar 30, 2014 at 15:48

3 Answers 3

1

In PHP use this to get the string sent with ajax :

$data = file_get_contents("php://input");

And in JS :

dataString = txCryptData;
var xhr = new XMLHttpRequest();
var params="data="+dataString;
xhr.onreadystatechange = function() {
 if (xhr.readyState == 4 && xhr.status==200) {
       alert(xhr.responseText);
     }
}
xhr.open('POST', 'tokenize.php', true);
xhr.send(params); 
Sign up to request clarification or add additional context in comments.

1 Comment

Ya I changed,this is what I used in php $data = file_get_contents("php://input"); echo $data;
1
    $.ajax({
    type: 'POST',
    // make sure you respect the same origin policy with this url:
    url: 'youUrl',
    data: { 
        'Joo': 'bar', 
        'ca$libri': 'no$libri' //  $ sign in the parameter name seems unusual, I would avoid it
    },
    success: function(msg){
        alert('Value' + msg);
    }
});

Comments

0

xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xhr.send(params); You may have to add these lines to your js snippet

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.