Toon posts:

[JavaScript] xmlhttprequest post

Pagina: 1
Acties:

Verwijderd

Topicstarter
Wanneer ik data probeer te posten via XmlHttpRequest, komt de data niet goed aan wanneer er een & teken in de string zit.

content=xmlPostRequest (location,"content=Goedemiddag ik ben ge�nteresseerd in.");

komt dus aan in mijn PHP script als

'content' => 'Goedemiddag ik ben ge'

Ik wil de amperstamp dus in feite escapen. Nu had ik al een HTMLencode functie gemaakt, maar ook die werkt niet. Dit is mijn code. Hopelijk weet iemand een oplossing.

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function HTMLEncode ( text )
{
    if ( typeof( text ) != "string" )
        text = text.toString() ;
    
    text = text.replace(/&/g, "&") ;

    return text ;
}

function xmlGetRequest (location) {
    var basename = parent.main.document.getElementsByTagName('base')[0].href;
    
    xmlRequest = new XMLHttpRequest();
    xmlRequest.open('GET',basename + location,true);
    xmlRequest.send(null);
}

function xmlPostRequest (location,data) {
    var basename = parent.main.document.getElementsByTagName('base')[0].href;
    
    xmlRequest = new XMLHttpRequest();
    xmlRequest.open('POST',basename + location,true);
    
    xmlRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    data=HTMLEncode(data);
    
    alert (data);
    xmlRequest.send(data);
}

Verwijderd

Kijk eens op http://xkr.us/articles/javascript/encode-compare/ voor JavaScript methodes om tekst te escapen.

Verwijderd

Topicstarter
Hartelijk bedankt, encodeURIComponent() was de functie die ik nodig had! Nu kan ik weer verder!