如何在服务器文件(如php)不存在时创建ajax请求?
i was developing sharetronix script but there is something realy odd about this script i can see in console network request goes for example for this address
http://localhost/ajax/postform-submit/ajaxtp:xml/r:0
but i cant find any folder with postform-submit name.i know this is possible with redirection using htaccess but i cant find anything in htaccess file.
this is part of js code where this request send
function postform_submit_step4()
{
var req = ajax_init(true);
if( ! req ) { return; }
var p = "post_temp_id="+encodeURIComponent(pf_data.temp_id)+"&message="+encodeURIComponent(pf_data.message);
if( pf_data.existing_post_id != "" ) {
p += "&editpost="+encodeURIComponent(pf_data.existing_post_id);
}
else if( pf_data.share_with_type == "user" ) {
p += "&username="+encodeURIComponent(pf_data.share_with_xtra);
}
else if( pf_data.share_with_type == "group" ) {
p += "&groupname="+encodeURIComponent(pf_data.share_with_xtra);
}
if( pf_data.at_link[0] ) {
p += "&at_link="+encodeURIComponent(pf_data.at_link[0]);
}
if( pf_data.at_image[0] ) {
p += "&at_image="+encodeURIComponent(pf_data.at_image[0]);
}
if( pf_data.at_file[0] ) {
p += "&at_file="+encodeURIComponent(pf_data.at_file[0]);
}
if( pf_data.at_videoembed[0] ) {
p += "&at_videoembed="+encodeURIComponent(pf_data.at_videoembed[0]);
}
req.onreadystatechange = function() {
if( req.readyState != 4 ) { return; }
if( ! req.responseXML ) { return; }
var data = req.responseXML.getElementsByTagName("result");
if( !data || !data[0] ) { return; }
data = data[0];
var status = data.getElementsByTagName("status");
var message = data.getElementsByTagName("message");
if( !status || !status[0] || !message || !message[0] ) {
return;
}
status = status[0].firstChild.nodeValue;
message = message[0].firstChild.nodeValue;
if( status != "OK" ) {
d.getElementById("pf_postederror_msg").innerHTML = message;
postform_htmlobject_hide("pf_posting");
postform_htmlobject_show("pf_postederror", 36);
postform_htmlobject_show("pf_mainpart", 114, function() { pf_open_state=1; pf_post_state=3; d.post_form.message.disabled=false; d.post_form.message.focus(); });
return;
}
d.getElementById("pf_postedok_msg").innerHTML = message;
postform_htmlobject_hide("pf_posting");
postform_htmlobject_show("pf_postedok", 36, function() { pf_open_state=0; pf_post_state=1; postform_statusmsg_setTimeout(); });
var btn = d.getElementById("postform_open_button");
if(btn) {
btn.style.display = "";
}
if( posts_synchronize ) {
posts_synchronize();
}
var pinf = pf_data.existing_post_id;
if( pinf != "" ) {
pinf = pinf.split("_");
var tmp = w.location.href.toString();
tmp = tmp.replace(/^http(s)?\:\/\//, "");
tmp = tmp.substr(tmp.indexOf("/"));
var mtch = "/view/"+(pinf[0]=="public"?"post":"priv")+":"+pinf[1];
if( tmp.substr(0,mtch.length)==mtch ) {
if( viewpost_synchronize ) {
viewpost_synchronize();
}
else {
w.location.href = w.location.href.toString();
}
}
}
}
req.open("POST", siteurl+"ajax/postform-submit/ajaxtp:xml/r:"+Math.round(Math.random()*1000), true);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.send(p);
}
how is this possible when there is no redirection?
The script is not odd: the way an URL maps to a file not always is so straightforward.
Most web frameworks, for example, use a router/dispatcher to decide what to do/which file to serve given a specific URL.
For example, most MVC web frameworks by default would handle this URL:
http://www.example.com/users/edit/5
by calling the action/method "edit" of the controller "users" passing an argument 5.
Long story short, redirects are not the only way an URL can be mapped to a physical file on the webserver. To find which file is served or which file contains the code that is executed to produce what you receive, you first need to know which application server/framework is used, and learn how it works.