Jump to content

Getting entire webpage in AJAX GET


publicGenome

Recommended Posts

Hi There,

 

This is my first post in the forum kindly pardon me if I've violated any forum rules.

 

I've a form, enter something, hit submit, data gets inserted into the database using AJAX POST call.

I've been trying to pull data from PHP and display, using AJAX GET. But unfortunately, I get complete web page. Data I'm sending/echo-ing is in JSON. I tried exit() in the PHP "select *", but it didn't help.

 

Below are my code snippets:

 

Form code:

<form id="randomInsert" action="<?php echo URL;?>dashboard/xhrInsert" method="post">
<input type="text" id="name_form" name="name_text"/>
<input type="submit" id="name-submit" />

</form>

Model code:

	public function xhrGetListings(){

		$sth=$this->db->prepare('select * from data');
		$sth->setFetchMode(PDO::FETCH_ASSOC);
		$sth->execute();
		
		echo json_encode($sth->fetchAll()) ;

//		return $sth->fetchAll();
	}

Java script code:

		$.get('dashboard/xhrGetListings',function(data){
			
			console.log(data);
			alert(JSON.stringify(data));
		});
		
	//});
	
	
	$('#randomInsert').submit(function(){
			
			var url=$(this).attr('action');
			var data=$(this).serialize();
			$.post(url,data, function(o){	

			});

			return false;
			});
Link to comment
Share on other sites

When you say "But unfortunately, I get complete web page." does that mean your are getting HTML/CSS along with your json data or no json data at all?

 

Also can you give information about your php application. Is it completely custom or are you using a specific framework? If its custom how are routing the request for 'dashboard/xhrGetListings'?

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.