4

I need to read data from my web page with jquery + ajax

This is my function:

public function getvalueshahr()
{
if($_POST['ostan']!=0){
$db=  JFactory::getDbo();
$query=$db->getQuery(TRUE);
$query->select('id,title')->from('#__categories')->
where($db->quoteName('parent_id').'='.$db->quote($_POST['ostan']));
$db->setQuery($query);
$res=$db->loadObjectList();
echo $db->getErrorMsg();
echo json_encode($res);}
}

I can read data with c# like this:

enter image description here

But my ajax method is not working. Here is the method:

 $.ajax({
   type: "POST",
   url: "www.mysite.net/index.php?task=shahrestan.getvalueshahr",
  data: "{ostan=77}",
  dataType: "json",
  success: function (result)
  {
    var d = $.parseJSON(result);
    $.each(d, function (i, field)
  {
    $("#output").append("id: " + field.id + " title: " +  
    field.title+"br/>"); 
  });
  },
    error: function (e)
  {
    alert("error:" + e.responseText);
  }
 });     

The method returns nothing.

3
  • You should really learn how to write good code, your code looks very messy and is hard to read. Commented Apr 21, 2016 at 6:48
  • thanks any book or ebook Commented Sep 22, 2016 at 14:36
  • What do you mean? Commented Sep 22, 2016 at 14:45

3 Answers 3

2

You're sending a string instead of an object in your ajax-request. Try changing :

data: "{ostan=77}"

to

data: { ostan: 77 }

And you should set the content type before echoing the results in PHP:

header('Content-Type: application/json');
echo json_encode($res);

Now you don't need $.parseJSON in the JS-code. You will get a json-object straight away.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, it returns an error message and e.responseText is empty how can i get error details
Have you tried console.log(e) instead of alert(e.responteText) when catching the error? Use your browsers console to see the output. Here's how to open it in different browsers: webmasters.stackexchange.com/questions/8525/…
0

First of all you send a wrong data to the server. You have to rewrite part of your code like this:

data: {ostan: 77},

or

data: "ostan=77",

The second problem is you get a result as a json object on the success method, but you try to parse it again to the json object. So remove this line from your code:

var d = $.parseJSON(result);

Finally your ajax function should looks like this:

$.ajax({
   type: "POST",
   url: "www.mysite.net/index.php?task=shahrestan.getvalueshahr",
  data: {ostan: 77},
  dataType: "json",
  success: function (result)
  {
    $.each(result, function (i, field)
  {
    $("#output").append("id: " + field.id + " title: " +  
    field.title+"br/>"); 
  });
  },
    error: function (e)
  {
    alert("error:" + e.responseText);
  }
 });

2 Comments

thanks, it returns an error message and e.responseText is empty how can i get error details
Add a "complete" method to your ajax to get all data about server error. You have to write like this example: complete: function (res) { console.log(res); } Run your codes on the chrome browser, than from the console copy and paste the output result please
0

If you are loading data from you mysql-database you eventually have to convert these data because it is not stored as utf-8 converted. This piece of code convert given object to UTF-8: mb_convert_encoding(database-entry,'UTF-8');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.