Jump to content

[SOLVED] Ajax problem


Stooney

Recommended Posts

So I am having an issue with using my ajax function to call a page starting with http://.  I'm not sure how it all works, but here's what I'm working with.  My site uses mod_rewrite.  It used to work fine when called the file like ajaxReq('../ajax/test.php') but it seems as if http:// broke it.  And due to how the mod_rewrite works I can't call the file directly.

 

I am using an mvc setup, so where you will see http://site.com/ajax/request/test it's designed like http://site.com/controller/action/ajax_filename.  My ajax controller takes the name and includes the proper file which produces the output for the ajax request like 'test|testvar'.

 

I am receiving this javascript error: Cannot set property 'innerHTML' of null

 

Will an http:// url not work here? Or am I just missing something?  Also, any comments on the design of how it works?  I'm not by any means ajax savvy.

 

index.php

<div id="test"></div><input type="button" value="click" onClick="ajaxReq('http://site.com/ajax/request/test?var=testvar')">

 

ajax.js

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function ajaxReq(action) {
    http.open('get', action);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|' != -1)) {
            update = response.split('|');
            document.getElementById(update[0]).innerHTML = update[1];
        }
    }
}

 

test.php

echo 'test|'.$_GET['testvar'];

Link to comment
https://forums.phpfreaks.com/topic/127800-solved-ajax-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.