AJAX post, how to encode?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: AJAX post, how to encode?

Hybrid View

  1. #1
    Join Date
    Sep 2008
    Location
    The Netherlands
    Posts
    22

    AJAX post, how to encode?

    Hello everybody,

    I guess my main question is; How do i encode HTML data from a textarea to prepare it for an AJAX call ( post method ).

    I'm trying to insert some html code in a record in my database via a ajax http request. I'm using the POST method. The html that i'm trying to insert is in a textarea ( CKEDITOR: WYSIWYG ) and it seems to be working. But when i change some mark-up in the textarea, like making a word Strong. The AJAX functions returns a 403 Forbidden page.
    I don't understand this because there are also <p></p> surrounding the data but that doesn't give an error. Only if i change to mark-up some more and add more HTML to the data then the AJAX call gives the 403 response page.

    What is the best way to solve this problem. Must i encode all data before the AJAX call?.
    This is my code.

    Code:
    var p = "hiddenSEOID="+document.editProductForm.hiddenSEOID.value;
    	p += "&productSEOID="+document.editProductForm.productSEOID.value.toLowerCase();
    	p += (document.editProductForm.prodAlive.checked) ? "&prodAlive=1" :"&prodAlive=0" ;
    	p += "&prodName="+document.editProductForm.prodName.value;
    	p += "&prodRange="+document.editProductForm.prodRange.value;
    	p += "&prodDesc="+CKEDITOR.instances.prodDesc.getData();
    	p += "&categories="+EL.compile("categoriesEL");
    	p += "&prodKeywords="+EL.compile("keywordsEL");
    
    	alert(p);
    	
    	var xhr = xmlHttpRequest();
    	xhr.open("POST","php/updateProductBySEOID.php", true);	
    	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	xhr.setRequestHeader("Content-length", p.length);
    	xhr.setRequestHeader("Connection", "close");
    	
    	xhr.onreadystatechange = function(){
    		
    		if(xhr.readyState==4){
    			
    			alert(xhr.responseText);
    			
    		}
    		
    	}
    	
    	xhr.send(p);

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,868

    Re: AJAX post, how to encode?

    Make sure you are using encodeURIComponent() on your variables for p.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Sep 2008
    Location
    The Netherlands
    Posts
    22

    Re: AJAX post, how to encode?

    OK thanks, but aren't special chars automaticly encoded by the POST call ?. or is this insufficient.

  4. #4
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,868

    Re: AJAX post, how to encode?

    Quote Originally Posted by TimothyH View Post
    but aren't special chars automaticly encoded by the POST call ?
    Nope.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center