ajax: permission denied error

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

ajax: permission denied error

Post by raghavan20 »

i have a very simple page, ajaxTest.php which has a form to insert a scrap. it includes two files, core.js and scrap.js. the form sends poster and message values to insertScrap method in scrap.js and scrap.js makes an appropriate POST request to a core method sndReq() in core.js. The core.js actually send the POST request to a PHP file, PW_C_Scrap.js and it will process it and return text.

but i have been getting the following message when xmlhttp opens a POST request, 'Error: uncaught exception: Permission denied to call method XMLHttpRequest.open';

core.js

Code: Select all

		
var http = createRequestObject();

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




function sndReq(sendMethod, url, event, parameters) { 

	alert( 'sendMethod: ' + sendMethod + ' url: ' + url + ' event: ' + event );
	//informToWait( 'AJAX Message: Please wait until content loads ...' );
    http.open( sendMethod, url, true );
    if( sendMethod == 'post' ){
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", parameters.length);
		http.setRequestHeader("Connection", "close");
		http.send(parameters);
    }
    
    http.onreadystatechange = function(){
      alert( 'http state: ' + http.readyState + 'event: ' + event );
	  if(http.readyState==4){
	    	if( event == 'insertScrap' ){
	    		document.getElementById( 'scrapContent' ).innerHTML = http.responseText;
	    	}
	    }
	  }
    http.send(null);
    
}			

[/script]


scrap.js
[script]
	function insertScrap( poster, msg ){
		
		alert( poster + '-' + msg );
		var parameters = 'poster=' + encodeURI( poster ) + '&msg=' + encodeURI( msg );
		alert( parameters );
		sndReq( 'post', 'http://www.thisisraghavan.com/home/controllers/PW_C_Scrap.php?action=insert', 'insertScrap', parameters );
		
	}
	
	function informToWait( input ){
		document.getElementById( 'output' ).innerHTML = input;
	}
	

[/script]
ajaxTest.php

Code: Select all

	<script src="views/javascript/core.js"></script>
	<script src="views/javascript/scrap.js"></script>

<div>
<div style = "font-weight:bold; padding:4px">Scrap Book</div>
<div style = "border:1px solid black; width:200px;"></div>

<div id = 'insertScrapForm'>
	<form>
		<table>
			<tr>
				<th>Name</th><td><input type='text' name = 'poster' value = '' /></td>
				<th>Message</th><td><input type='text' name = 'msg' value = '' /></td>
				<th></th><td><input type="button" onclick="insertScrap( this.form.poster.value, this.form.msg.value )" value="Post Scrap" /></td>
			</tr>
		</table>
	</form>
</div>

<div id = 'scrapContent' style = 'overflow:scroll;'>

</div>
</div>

PW_C_Scrap.php

Code: Select all

<?php

##session_start();
##error_reporting( E_ALL );

$action = ( ( isset( $_REQUEST['action'] ) === TRUE ) ? $_REQUEST['action'] : '' );
$output = '';

print_r( $_REQUEST );


switch( $action ){
	
	case 'insert':{
		echo $_POST['poster'] . 'posted' . $_POST['msg']; exit;
	}
	
}


?>
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

Code: Select all

function insertScrap( poster, msg ){
               
                alert( poster + '-' + msg );
                var parameters = 'poster=' + encodeURI( poster ) + '&msg=' + encodeURI( msg );
                alert( parameters );
                sndReq( 'post', 'http://www.thisisraghavan.com/home/controllers/PW_C_Scrap.php?action=insert', 'insertScrap', parameters );
} 
Try just doing the sndReq() to 'home/controllers/PW_C_Scrap.php?action=insert', or whatever relative path you need based on your current path. AJAX is unable to go cross-domain for security reasons, and it might see the domain aspect as not being your current domain.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

what you told me worked. thank you.

but i cannot understand how it works, because the request comes from the client and how is it able to communicate with the server without fully qualified url having domain name/ip.
Post Reply