6

I am having an issue with ZEND, JQuery, PHP, and Javascript. I am very new to these languages, and I am just trying to work my way around them.

I have had jquery.post work in other cases, but I cannot get it to do a simple call right now.

phtml File (/admin/user.phtml)

function testJquery()
{
var url = "<?php echo $myHome2Url.'/admin/newusercallback/sessionid/'.$_SESSION['mySessionId'].'/lang/'.$_SESSION['language']?>";
console.log(url);
$.post(url,{},
function(data)
{
    window.alert("Call successful!");
    window.alert(data);
},"json");

}

PHP File: (AdminController.php)

public function newusercallbackAction()
{
     echo json_encode("Test");
}

A /admin/newusercallback.phtml file exists.

  <?php ?>

When run, the console has the following messages printed to it:

POST https://dev.myurl.ca/admin/newusercallback/sessionid/0c1a5e41-ad0a-470d-9901-305464b48908/lang/ENG
https://dev.myurl.ca/admin/newusercallback/sessionid/0c1a5e41-ad0a-470d-9901-305464b48908/lang/ENG
GET https://dev.myurl.ca/admin/user/sessionid/0c1a5e41-ad0a-470d-9901-305464b48908/lang/ENG/report_id/0/

I have 2 popup boxes appearing, which say Call Successful! and Null

The issue is that the second popup box should say "Test" instead of Null.

If I browse to the URL https://dev.myurl.ca/admin/newusercallback/sessionid/0c1a5e41-ad0a-470d-9901-305464b48908/lang/ENG directly, my browser displays a window with the text

"Test"

(quotation marks inclusive)

My question is, why is the callback data for testJquery Null, when browing directly to the page is displaying the data "Test" correctly? Further to that, how can I fix it!

4
  • Try changing json_encode("Text"); to json_encode(array("result"=>"Test")); and window.alert(data); to window.alert(data.result); Commented Nov 6, 2012 at 0:50
  • @shadow json_decode('"Test"'); codepad.org/Slqt2Aaq, works also on node.js Commented Nov 6, 2012 at 0:57
  • Tried it and it works jsfiddle.net/9PnRQ Commented Nov 6, 2012 at 1:03
  • Odd. Wonder what I did differently. At any rate - what I suggested above is a much better way to use json, as it allows you to send extra info as well (in other elements) Commented Nov 6, 2012 at 1:59

3 Answers 3

2

Thats becouse your jQuery function asking for "json" format, and text "Test" is not json.

Solutions: 1. Remove "json" dataType on your testJquery() function:

    function testJquery(){
        $.post(url,{},function(data){
            window.alert("Call successful!");
            window.alert(data);
        });
    }

2. Edit your AdminController.php file to encode array object

    public function newusercallbackAction(){
        echo json_encode(array("Test"));
    }
Sign up to request clarification or add additional context in comments.

3 Comments

I thought that it became a json object when I json_encoded it. When I remove the "json" the object becomes undefinded. Adding in the json and encoding an array returns a null-array.
If you're going to put "json" on your jQuery function, make sure when you access the URL directly from browser will print samething like {"Key":"Test"} or ["Text"].
I'm not sure why this guy got a negative vote... from what I can tell he is on the right track. A string by itself is not JSON.
1

It looks like your controller is only responding to HTML/TEXT. I don't know how to do it, but your controller should return JSON

5 Comments

json_encode("Test") should be valid json php.net/manual/en/function.json-encode.php
I just tried it - it isn't. Also if you look at all of the examples, they use arrays. I was under the impression that json_encode was built just for arrays... Feel free to prove me wrong.
That json parser is actually a lot more lenient than most. Try it again in the link I posted before. Try it in the php function.
I tried out my existing code in IE, and it works there. It only fails in firefox and Chrome. adding the JSON.parse did not solve the problem.
From php.net/manual/en/function.json-encode.php: "json_encode() will generate JSON that is a simple value (that is, neither an object nor an array) if given a string, integer, float or boolean as an input value. While most decoders will accept these values as valid JSON, some may not, as the specification is ambiguous on this point." I just looked at RFC 4627 and json.org, and it didn't look very ambiguous -- the grammar requires an object or array at the root (but maybe the PHP doc predated the spec?).
1

It will only work when response header "Content-Type" is valid json mime, which is application/json. and it also happens to shorthand version .getJSON

put this in your php before you output anything else

header("Content-type: application/json; charset=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.