0

I have a ajax request as below :

$('#student_name').change(function(){
    var student_id = $(this).val();
    if(student_id!=0){

    var url = "ajax_student_bill_details.php";
    var data = "id="+student_id;

    $.ajax({
        type: "GET",
        url: url,
        dataType : "json",
        data: data,
       // cache: false,
       error : function(result){
                alert('Error');
                },
        success: function(result){
                alert(result);
        } 
    });
    }
});

And the ajax_student_bill_details is :

$arra = ('a' => 1,'b' => 2);
return json_encode($arra);

But when I change the options it always alerts Error . Whats wrong with my codes ?

DEMO

EDIT:

$arra = array('a' => 1,'b' => 2);

5
  • because your url is incorrect Commented Dec 28, 2013 at 7:52
  • same code with correct url jsfiddle.net/N5e66/3 Commented Dec 28, 2013 at 7:54
  • @rajeshkakawat how? I have a file called 'ajax_student_bill_details.php' in same directory Commented Dec 28, 2013 at 7:55
  • try to hit your ajax url in browser address bar see what you get???? Commented Dec 28, 2013 at 7:57
  • It displays nothing ( after editing the error)!! Why so ? var_dump would give string '{"a":1,"b":2}' (length=13). Thanks Commented Dec 28, 2013 at 8:07

2 Answers 2

1

you should echo your json

$arra = ('a' => 1,'b' => 2);
echo json_encode($arra);
Sign up to request clarification or add additional context in comments.

Comments

1

Not sure as I don't have access to your server and the fiddle was bad. I would suspect the missing protocol (http://) on your URL.

Here's a fiddle that uses http://jsontest.com and that works. Perhaps you can work from that. If you just replace your url perhaps you can see what issue is.

$('#student_name').change(function(){
var url = "http://echo.jsontest.com/key/value/one/two";
var data = "id=1";

$.ajax({
    type: "GET",
    url: url,
    dataType : "json",
    data: data,
    // cache: false,
    error : function(result){
     alert('Error');

    },
    success: function(result){
        alert('Ok');
    } 
});
});

1 Comment

yeah its working I can see. So the problem is ith the url I guess. Whats wrong with the json_encode for the above array in my question? It displays nothing when I tried to see what it returns

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.