SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: IE POST problem

  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    IE POST problem

    I have a small issue that Im having trouble trying to debug.

    The project is at http://www.acumendesign.co.uk/acumen_cms/news.php

    The problem im having is when a new article is saved. In firefox a alert appears after the article is saved, then returns back to the main article list.

    In IE when you click save it does nothing except save the story in the background. But not even the "saved" article alert does not appear!

    Any ideas?

    Code JavaScript:
     
    // SAVE ARTICLE
     
    function saveArticle(){
     
    	if($("title").value == ""){
    		alert("Please provide a title for this article");
    	} else {
     
    	var addCat = $("1");
    	addCat.setAttribute("onClick", "getNewsCat(this.id)");
     
    	var addCat = $("2");
    	addCat.setAttribute("onClick", "getNewsCat(this.id)");
     
    	var url="addnews.php";
        http.onreadystatechange = postArticle;
    	http.open("POST", url, true);
     
        //Send the proper header information along with the request
    	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	http.setRequestHeader("Connection", "close");
     
        var tinyContent = tinyMCE.get("copy").getContent();
        tinyContent = tinyContent.replace(/\+/g, "&#43");
        tinyContent = tinyContent.replace(/\\/g, "&#92");
        tinyContent = escape(tinyContent);
     
    	var poststr = "hiddenfield=" + $("hiddenfield").value + "&title=" + $("title").value + "&copy=" + tinyContent;
    	http.send(poststr);
    	return true;
    	}
     
    }
     
    // HANDLE POST ARTICLE
     
    function postArticle(){
     
        if((http.readyState == 4)&&(http.status == 200)){
     
    	new Effect.Fade("createarticle", { duration: 0.5, queue: "front"});
    	new Effect.Fade("cancel_bb", { duration: 0.5, queue: "end"});	
    	$("article").reset(); // RESET FORM
     
    	var responseId = http.responseText;
    	alert("Story is now saved");
     
    	getNewsCat(responseId);
    		}
    }

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your not having much luck are you lol

    can you first try removing:

    Code:
    http.setRequestHeader("Connection", "close");


  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No im not. Im nearly there though!

    I tried reducing the code all down to. Still no response!
    Code JavaScript:
    // SAVE ARTICLE
     
    function saveArticle(){
     
        if($("title").value == ""){
            alert("Please provide a title for this article");
        } else {
     
     
        var url="addnews.php";
        http.onreadystatechange = postArticle;
        http.open("POST", url, true);
     
        //Send the proper header information along with the request
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
     
        var tinyContent = tinyMCE.get("copy").getContent();
        tinyContent = tinyContent.replace(/\+/g, "&#43");
        tinyContent = tinyContent.replace(/\\/g, "&#92");
        tinyContent = escape(tinyContent);
     
        var poststr = "hiddenfield=" + $("hiddenfield").value + "&title=" + $("title").value + "&copy=" + tinyContent;
        http.send(poststr);
        return true;
        }
     
    }
     
    // HANDLE POST ARTICLE
     
    function postArticle(){
     
        if((http.readyState == 4)&&(http.status == 200)){
     
     
        alert("Story is now saved");
     
            }
    }

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol right, what I found was that postArticle isn't actually even being called, not even with an 404 or other status.

    Could you change

    Code:
    http.onreadystatechange = postArticle;
    to

    Code:
    http.onreadystatechange = function() {
        alert('Ready State Changed');
        postArticle();
    };
    Just to see whether somethings happening or not. It may be your Ajax code.

    Here is what I use daily, and how to implement it:

    Code:
    function createXMLHttpRequest() {
        if (typeof XMLHttpRequest != "undefined") {
            return new XMLHttpRequest();
        } else if (typeof ActiveXObject != "undefined") {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } else {
            throw new Error("XMLHttpRequest not supported");
        }
    }
    function doRequest(url, callback, postAttributes) {
        var request = createXMLHttpRequest();
        request.open(((postAttributes) ? 'POST' : 'GET'), url, true);
        if(postAttributes) {
            var postStr = '';
            if(typeof(postAttributes) != 'string') {
                for(var i in postAttributes) {
                    postStr += '&' + i + '=' + postAttributes[i];
                }
            } else
                postStr = postAttributes;
            request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            request.setRequestHeader("Content-length", postStr.length);
            request.setRequestHeader("Connection", "close");
        }
        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                if(request.status == 200)
                        callback(request);
                else {
                    alert('An error has occurred');
                }
            }
        }
        if(postStr)
            request.send(postStr);
        else
            request.send(null);
    }
    Code:
    var params = new Array();
        params['hiddenfield'] = $("hiddenfield").value;
        params['title'] = $("title").value;
        params['copy'] = tinyContent;
    
    doRequest('addNews.php', function(res) {
        alert('Story is now Saved');    
    }, params);
    You do not need to check for readyStates or status's as it checks this its self.

    To access the responseXML or responseText its simply

    Code:
    alert(res.responseXML);
    alert(res.responseText);


  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes I think there is a little glitch in my Ajax. I noted I was missing the "()" after postArticle. When I added it IE processed it but there was a error which firebug showed was to do with the header.

    Your method looks alot more efficient as it's only needed once and then called at the appropriate time.

    I tried it and it worked. Thanks for your input on that.

    Im going to get my head around the Prototype Ajax handler to hopefully eliminate future problem like this.

  6. #6
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you omit the parenthesis () from the function name when assigning via

    ajax.onreadystatechange = method;

    Your probably best off using the Protype Ajax handler.


  7. #7
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In IE you have to assign the onreadystatechange handler AFTER you have called the open method. (works in firefox too)

    ajax.open(...)
    ajax.onreadystatechange = ...
    ajax.send


    OK - having posted this I'm looking for this documented somewhere, and I can't find it. I could be wrong, or this could apply to old versions of the XMLHTTP activeX object in IE. If it fixes your problem - great, please let me know.

    Aha: http://www.robertnyman.com/2007/04/0...-call-allowed/
    Last edited by jimfraser; Apr 24, 2008 at 11:10. Reason: possible brain damage

  8. #8
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually I got my head around the Prototype Ajax handler which IE handles aswell.

    Thanks for the research, interesting article


Bookmarks

Posting Permissions

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