0

I am trying to use jQuery ajax to call PHP and return JSON.

My test is very simple, but all I seem to be getting is an empty object.

I am not getting any PHP errors in the LOG File.

jqXHR is shown as an object with the 'alert', but does not appear in the 'Chrome' console.

The 'Unpacked' alert does not show at all

I have obviously missed something or made some stupid error, but I can not see what I have done wrong.

HTML

<body>
<form id="frmSearch" onsubmit="matchQryString();">
    Search: <input type="text" name="qryWords" value="" />
   <input type="submit" name="submit" value="Search" />
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script>
    function matchQryString() {
        alert("Running AJAX");
        $.ajax({
            type:'POST',
            url: 'rtnJSON.php',
            data:$('#frmSearch').serialize(),
            dataType: 'json',
            success: function(response) {
                alert("PHP Return SUCCESS");
                alert(response);
            },
            error: function (jqXHR, exception) {
                alert("PHP Return ERROR");
                alert(jqXHR);
                alert(exception);
                console.log(jqXHR);
                var op = "";
                for (property in jqXHR) {
                    op += property + ': ' + obj[property]+'; ';
                }
                alert ("Unpacked: " + op);
            }
        });
       return false ;
    }
</script>
</body>

PHP

<?php

  $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

  header('Content-type:application/json;charset=utf-8');

  echo json_encode($arr); // {"a":1,"b":2,"c":3,"d":4,"e":5}

?>
1
  • 1
    obj[property] - where is obj defined? Commented Aug 23, 2015 at 15:20

1 Answer 1

1

form being submitted before $.ajax() call ? Try removing onsubmit from html , type="submit" from input element , attaching click event to type="submit" element , calling .e.preventDefault() within click handler . Also note, obj does not appear defined within error handler of $.ajax()

html

<form id="frmSearch"">
    Search: <input type="text" name="qryWords" value="" />
   <input type="button" name="submit" value="Search" />
</form>

js

$(function() {
      function matchQryString(e) {
        e.preventDefault();
        alert("Running AJAX");
        $.ajax({
            type:'POST',
            url: 'rtnJSON.php',
            data:$('#frmSearch').serialize(),
            dataType: 'json',
            success: function(response) {
                alert("PHP Return SUCCESS");
                alert(response);
            },
            error: function (jqXHR, exception) {
                alert("PHP Return ERROR");
                alert(jqXHR);
                alert(exception);
                console.log(jqXHR);
                var op = "";
                for (property in jqXHR) {

                    op += property + ': ' + obj[property]+'; ';
                }
                alert ("Unpacked: " + op);
            }
        });
    };

    $("[name=submit]").on("click", matchQryString);
})
Sign up to request clarification or add additional context in comments.

3 Comments

Many thanks. It did not work initially with e.preventDefault() at the start of the function matchQryString(). When I commented it out - it worked as I thought it would. Where should I position the e.preventDefault() call ?
@mcl "It did not work initially with e.preventDefault() at the start of the function matchQryString(). When I commented it out - it worked as I thought it would. Where should I position the e.preventDefault() call ? " If e.preventDefault() not needed to return expected result , leave omitted ?
Apologies : silly me missed the addition of e being passed to function

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.