开发者

AJAX response that i can't understand

开发者 https://www.devze.com 2023-02-23 00:50 出处:网络
I have 2 questions : If i send the ajax request to self page (from abc.php to abc.php) like this : $.ajax({

I have 2 questions :

If i send the ajax request to self page (from abc.php to abc.php) like this :

$.ajax({
   type: "POST",
   url: "some.php",
   data: "name=John&location=Boston",
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
 });

What will msg variable contain if I have a full webpage (with div's , forms, imgs) ? Will it contain the whole html code source ? How to tell ajax to return only specific details (like for example a php $variable after querying a database for a record based on Name and Location) . Remember some.php is the same file that contains the ajax script.

I want to make a .php script that contains all querys posible to manage a database like this:

if(isset($_GET['option']) && $_GET['option'] == 'insert') { code here .. and echo div`s .. etc) }
if(isset($_GET['option']) && $_GET['option'] == 'del') { code here .. and echo div`s .. etc) }
if(isset($_GET['option']) && $_GET['option'] == 'update') { code here .. and echo div`s .. etc) }
if(isset($_GET['option']) && $_GET['option'] == 'find') { code here .. and echo div`s .. etc) }
if(isset($_GET['option']) && $_GET['option'] == 'abc') { code here .. and echo div`s .. etc) }

and i want to run ajax request based on option and retrive specific results (like php $variables ... and so on)

How do i do that ?

Anyway the most important question is how to i get ajax.result that contain开发者_如何学C just a php $variable or an $array if the page that receive ajax request already contains <html><body><divs><tables><h4><h3>.... etc ?

Because jquery.ajax() has a documentation quite complex/complicated on it's options like accepts, async, complete, contentType, context .. and so on.

?

Thanks a lot.


You can use the dataType option to specify the type of data that you are expecting back from the server. In your case JSON might be a good option since the php page can return a String in the form:

  {name:value}

that that is easy to process as a JSON object on the client.

e.g. Found on this page

 $.ajax({
   url: url,
   dataType: 'json',
   data: data,
   success: callback
 });

Your callback function will then process the JSON and retrieve the value. As you can see from the link, you can use the getJSON() method as a shortcut.


The response variable msg will contain the full html of the page you have requested. To get a php variable instead, you need to pack up the variable in a way that javascript can understand, which usually means serializing the object / variable in a json format and then using $.parseJSON or eval on msg to turn the resultant string into a javascript object / array.


The answer is, don't make an ajax request to a page that already contains <html><body etc, use a page without that stuff.

All you need is

echo $variable;
0

精彩评论

暂无评论...
验证码 换一张
取 消