/    Sign up×
Community /Pin to ProfileBookmark

AJAX: problem sending data

Hi, I’m new to ajax and I hope someone could help me out here. It’s very frustrating that I cannot see anything wrong with my code and still it doesn’t work 😡 . I just updated the source code a little bit from mozilla site so that instead of having everything in one place I can break it down to small functions.The link for that is here [url]http://developer.mozilla.org/en/docs/AJAX:Getting_Started[/url]

For some reason the “postData(…) function is not executing. I couldn’t find what the problem is. If anyone could show me the mistake, that’ll be really helpful. Thanks a lot and Here’s my code:

[code=html]<input type=”button” value=”submit” onclick=”submitData()” />[/code]
[CODE]
function makeRequest()
{
var http_request = false;

if (window.XMLHttpRequest)
{ // Mozilla, Safari,…
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType)
{
http_request.overrideMimeType(‘text/xml’);
// See note below about this line
}
}
else if (window.ActiveXObject)
{ // IE
try
{
http_request = new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch (e)
{
try
{
http_request = new ActiveXObject(“Microsoft.XMLHTTP”);
} catch (e) {}
}
}

if (!http_request)
{
alert(‘Giving up 🙁 Cannot create an XMLHTTP instance’);
return false;
}
else
return http_request ;
}

function prepareData()
{
// get the data for the files

var titleData = document.getElementById(‘titleData’).innerHTML ;

var data = ‘var1=title.txt&data=’+ titleData ;
return data ;
}

/**
* This function will send the data to a given url
* after the request was made for ajax.
*
* @param ajaxRequest – The object that made request to the server
* @param data – The data that will be sent to the server
* @param url – The url of the file that will accept the data
*/
function postData(ajaxRequest, data, url)
{
ajaxRequest.open(‘post’, url, true);
ajaxRequest.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded’);
ajaxRequest.onreadystatechange = function () { ajaxResult(ajaxRequest) ; } ;
ajaxRequest.send(data);
}

/**
* This function will show the output of the ajax request, which will
* be from the file / url that was connected to. The output will be
* written to a div that is already formatted using css
*
* @param ajaxRequest – The object that made request to the server
*/
function ajaxResult(ajaxRequest)
{
if(ajaxRequest.readyState == 1)
{
//You can add animated gif while loading //
document.getElementById(‘status’).innerHTML = ‘Loading…’;
}
if(ajaxRequest.readyState == 4)
{
var data = ajaxRequest.responseText;
document.getElementById(‘status’).innerHTML = data;
}
}

function submitData()
{
var requst = makeRequest() ;
var prepData = prepareData() ;
//alert(requst) ;
var url = ‘file.php’ ;
//alert(url) ;
postData(request, prepData, url) ;
}[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceMay 08.2006 — You, apparently, haven't checked for error messages. The following contains an error:
function submitData()
{
var requst = makeRequest() ;
var prepData = prepareData() ;
//alert(requst) ;
var url = 'file.php' ;
//alert(url) ;
postData(request, prepData, url) ;
}
Copy linkTweet thisAlerts:
@KramyMay 09.2006 — For some reason the "postData(...) function is not executing. I couldn't find what the problem is. If anyone could show me the mistake, that'll be really helpful. Thanks a lot and Here's my code:[/QUOTE]
var [b]requst[/b] = makeRequest();

postData([b]request[/b], prepData, url);

Develop with Firefox - it has a spiffy Javascript Console that would find that error and point you to the right line in a snap.
Copy linkTweet thisAlerts:
@parvezauthorMay 09.2006 — var [b]requst[/b] = makeRequest();

postData([b]request[/b], prepData, url);

Develop with Firefox - it has a spiffy Javascript Console that would find that error and point you to the right line in a snap.[/QUOTE]


oh my god. I knew that it was a stupid mistake but I just couldn't find it. hate it when this happens :mad: . I wasted about 1 hour to find the problem. Thank you Karmy for pointing it out. Thanks a lot. Also, I did use firefox's javascript console, but for some reason the error didn't show up in there. Oh well... thanks again
×

Success!

Help @parvez spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...