-2

I want to send a string and return json.

This works but it doesn't return json code

$.ajax({
    url: "getFeed.php",
    type: "post",
    data: myString
});

When I want it to return a json string, it gives me an error.

$.ajax({
    url: 'getFeed.php',
    type: "post",
    data: {string1: "testdata", string2: "testdata"},
    dataType: 'jsonp',
    jsonp: 'jsoncallback',
    timeout: 5000,
});

For some reason the server doesn't recieve any data.

How am I supposed to write this? (i want to send 2 strings)

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Michiel
  • 123
  • 1
  • 9

3 Answers3

1

JSONP does not allow using POST method as it is really just a script request.

Since path used is relative (same domain) I suggest you use json datatype since jsonp is intended for cross domain requests.

charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

EDIT: The keys are not, in fact, required to be in quotes. I was mistaken there. However, it still seems to be best practice, to avoid potential future issues.

$.ajax({
    "url": "getFeed.php",
    "type": "post",
    "data": {"string1": "testdata", "string2": "testdata"},
    "dataType": "jsonp",
    "jsonp": "jsoncallback",
    "timeout": 5000,
});
James Spence
  • 2,020
  • 1
  • 16
  • 23
  • No actually i need to use it like this: `data: {username: usernameVar, userID: userIDVar},` – Michiel Feb 27 '15 at 21:19
  • then it would be `"username": usernameVar, "userID": userIDVar`. "username" and "userID" are strings. – James Spence Feb 27 '15 at 21:24
  • 1
    quotes are not needed on object keys in javascript as long as there are no special characters in property names. Thus this answer is invalid – charlietfl Feb 27 '15 at 21:34
  • @charlietfl Really? I never knew this. Any chance you could point me to some reference? – James Spence Feb 27 '15 at 21:38
  • @charlietfl The first result returned from just googling around... http://stackoverflow.com/questions/4201441/is-there-any-practical-reason-to-use-quoted-strings-for-json-keys it sounds like it's not _required_, but is best practice EDIT: It also looks like jquery has, as of jquery 1.4, required double-quoted keys on the JSON, or else it silently throws an error. References http://api.jquery.com/jquery.parsejson/ and https://forum.jquery.com/topic/json-on-jquery-1-4 – James Spence Feb 27 '15 at 21:41
  • don't confuse a javascript object with JSON. The data option of ajax is an object – charlietfl Feb 27 '15 at 21:42
  • JSON is an object too. I'm afraid I don't understand the difference. And in this case, the double quotes would very clearly convey that the keys are not javascript _variables_, but are meant to be taken as string literals. – James Spence Feb 27 '15 at 21:45
  • no, JSON is a string data transfer format. Object keys are not variables either, they are keys. You are creating an object literal that subsequently will be converted to form encoding and not even JSON – charlietfl Feb 27 '15 at 21:48
  • Fair enough, I suppose this isn't JSON. Even in javascript objects, however, it's considered best practice to enclose keys in quotes. See [link](http://stackoverflow.com/questions/2788236/single-quotes-in-javascript-object-literal) as well as [Mathias Bynens' comment on the accepted answer](http://stackoverflow.com/questions/4348478/what-is-the-difference-between-object-keys-with-quotes-and-without-quotes) for reference. – James Spence Feb 27 '15 at 22:00
  • still subjective, look at objects in jQuery style guide http://contribute.jquery.org/style-guide/js/#object-and-array-expressions `Property names only need to be quoted if they are reserved words or contain special characters:` – charlietfl Feb 27 '15 at 22:05
0

Ok, first your ajax syntax doesnt seem to be right.

Lets suppose you want to send two strings stringA and stringB. Your php file is getFeed.php.

Your ajax would look like this:

    $.ajax({
               type : "POST",
               url : "getFeed.php",
               data : {firstVar:stringA,secondVar:stringB},
               dataType: "json",
               success : function(data){

                         // Here you receive the JSON decoded.
                        var mesage = data.msg;
                 }  
        });

Inside getFeed.php you would receive the strings like this:

$firstString = $_POST['firstVar'];
$secondString = $_POST['secondVar'];

And when you return the JSON object to the ajax from getFeed.php, this is how you would do it:

$myArray = array("msg"=>"Hello World", "another"=>"thing");
echo json_encode($myArray);

So lets go back to the ajax function, where the success part is, you receive a parameter data, and if you want to access the 'msg' and 'another', you would so like this:

       success : function(data){

                         var first = data.msg;
                         var second = data.another;
               }  
nullwriter
  • 785
  • 9
  • 34