www.webdeveloper.com
Results 1 to 6 of 6

Thread: ajax post request does not send the paramters.

  1. #1
    Join Date
    Nov 2005
    Location
    sweden
    Posts
    131

    ajax post request does not send the paramters.

    When im trying to send a post request to a php page i have written it dosent send the actual post parameters

    Code:
    var XmlHttpObj = {
        
        requestObj: null,
        parameters: [],
        
        init: function() {
            try
            {
                // Firefox, Opera 8.0+, Safari
                this.requestObj = new XMLHttpRequest();
            }
            catch (e)
            {
                // Internet Explorer
                try
                {
                    this.requestObj =new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e)
                {
                    try
                    {
                        this.requestObj =new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch (e)
                    {
                        alert("Your browser does not support AJAX!");
                    }
                }
            }
            
            this.requestObj.onerror = this.onError;
            this.requestObj.onreadystatechange = this.onReadyStateChange;
        },
        
        onError: function() {
            
        },
        
        onReadyStateChange: function() {
            
        },
        
        createRequest: function(method, url) {
            this.requestObj.open(method, url, true);
            
            if(method == 'POST') {
                this.requestObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");    
            }
            
            this.requestObj.overrideMimeType('text/xml');
            //this.requestObj.setRequestHeader('Expires',' Sat, 26 Jul 1997 05:00:00 GMT');
            //this.requestObj.setRequestHeader('X_REQUESTED_WITH', 'XmlHttpRequest');
            this.requestObj.setRequestHeader('Connection', 'closed');
            return this;
        },
        
        setCallback: function(callback) {
            this.requestObj.onreadystatechange = callback;
            
            return this;
        },
        
        setRequestParameters: function(parameters) {
            this.parameters = parameters;
            
            return this;
        },
        
        setRequestHeader: function(header, value) {
            this.requestObj.setRequestHeader(header, value);
            
            return this;
        },
        
        sendRequest: function() {
            paramLen = this.parameters.length;
    
            if(paramLen != 0) {
                this.setRequestHeader('Content-type','application/x-www-form-urlencoded');
                this.setRequestHeader("Content-length", paramLen);
                this.requestObj.send(this.parameters);
            } else {
                this.requestObj.send(null);
            }
            
            return this;
        },
        
        getResponse: function() {
            var headers = this.requestObj.getAllResponseHeaders();
            responseType = headers.substring(headers.lastIndexOf(':')+2);
            
            return this.requestObj.responseXML;
        }
    };
    
    XmlHttpObj.init();
        // Simple test
    XmlHttpObj.createRequest('POST', 'chat.php')
              .setRequestParameters({message: 'hello world'})
              .setCallback(function() {
                if(XmlHttpObj.requestObj.readyState == 4) {
                    alert(XmlHttpObj.getResponse());
                }
              }).sendRequest();

  2. #2
    Join Date
    Jul 2008
    Posts
    152
    Code:
        createRequest: function(method, url) {
            this.requestObj.open(method, url, true);
            
            if(method == 'POST') {
                this.requestObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");    
            }
            
            this.requestObj.overrideMimeType('text/xml');
            //this.requestObj.setRequestHeader('Expires',' Sat, 26 Jul 1997 05:00:00 GMT');
            //this.requestObj.setRequestHeader('X_REQUESTED_WITH', 'XmlHttpRequest');
            this.requestObj.setRequestHeader('Connection', 'closed');
            return this;
        },
    Why are you closing the connection at the very last line? I'm just confused overall why your Ajax script is so complicated...

    My ajax object:
    Code:
    		// Determine where get/post parameters go
    		if(this.method == "GET" && this.params != null)
    			this.request.open(this.method, this.action+"?"+encodeURI(this.params), this.async);
    		else
    			this.request.open(this.method, this.action, true);
    
    		// Set content header for json
    		this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    
    ...
    
    		// Send the request differently if there is POST data
    		if(this.method == "POST" && this.params != null)
    			this.request.send(this.params);
    		else
    			this.request.send(null);
    I don't know exactly why yours isn't sending, but above is what I use flawlessly.

  3. #3
    Join Date
    Aug 2007
    Posts
    151
    as I know, when you use a "POST" method of XMLHttpRequest, the parameters - xhr.send(parameters) - must be a query-string (like as x=abc&xx=123...) it can't be an object in json format.
    The classic script: alert("Hello world!")

  4. #4
    Join Date
    Jul 2008
    Posts
    152
    Quote Originally Posted by hoangkc
    as I know, when you use a "POST" method of XMLHttpRequest, the parameters - xhr.send(parameters) - must be a query-string (like as x=abc&xx=123...) it can't be an object in json format.
    Yes that is entirely correct.

  5. #5
    Join Date
    Nov 2005
    Location
    sweden
    Posts
    131
    Oh ok guys great ill try that

  6. #6
    Join Date
    Jan 2008
    Posts
    273

    AJAX: page not found when using POST instead of GET

    Hi All

    I noticed that my ajax calls were cached by the browser. However I want to do an ajax call over and over again, retrieving the data from the server, not from the browser's cache

    I red somewhere that you can change 'GET' into 'POST', because the browser doesn't cache POST-requests.

    Here is my code (snippet):
    Code:
    xmlDoc = new window.XMLHttpRequest();        
    xmlDoc.open("GET", "config.xml", false);
    xmlDoc.send(null);
    changing 'GET' to 'POST' results in a page not found (404) error. Any suggestions?

    Thnx a lot
    LuCa

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles