質問

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

役に立ちましたか?

解決

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

他のヒント

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

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top