Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery ajax request with json response, how to?

I am sending an ajax request with two post values, the first is "action" which defines what actions my php script has to parse, the other is "id" which is the id of the user it has to parse the script for.

The server returns 6 values inside an array() and is then encoded to JSON with the PHP function: json_encode();

Some of my responses are HTML, but when I encode it to JSON, it escapes "/" so it becomes "\/"
How do I disable that?

Also when I don't know how to display this in jQuery when I get the server response, I just thought that putting it all into a div would just display the numbers and HTML codes I had requested, but it displays the array as it is encoded in PHP.

PHP

$response = array(); $response[] = "<a href=''>link</a>"; $response[] = 1; echo json_encode($response); 

jQuery:

$.ajax({     type: "POST",     dataType: "json",     url: "main.php",     data: "action=loadall&id=" + id,     complete: function(data) {         $('#main').html(data.responseText);     } }); 

How do I make this into working JSON?

like image 729
Flaashing Avatar asked Feb 01 '12 15:02

Flaashing


People also ask

How do I get ajax response in JSON?

Approach: To solve this problem, we will first consider a JSON file named “capitals. json” and try to get this JSON data as a response using AJAX. Then we will create an HTML file “capitals. html” which contains a table which we will use to populate the data we are getting in response.

How pass JSON object in post request in ajax?

ajax({ url: , type: "POST", data: {students: JSON. stringify(jsonObjects) }, dataType: "json", beforeSend: function(x) { if (x && x. overrideMimeType) { x. overrideMimeType("application/j-son;charset=UTF-8"); } }, success: function(result) { //Write your code here } });

What does JSON parse () method do when we initiate an ajax request?

Description: Takes a well-formed JSON string and returns the resulting JavaScript value.

Can you use JSON with ajax?

According to the AJAX model, web applications can send and retrieve data from a server asynchronously without interfering with the display and the behavior of the existing page. Many developers use JSON to pass AJAX updates between the client and the server.


2 Answers

You need to call the

$.parseJSON(); 

For example:

... success: function(data){        var json = $.parseJSON(data); // create an object with the key of the array        alert(json.html); // where html is the key of array that you want, $response['html'] = "<a>something..</a>";     },     error: function(data){        var json = $.parseJSON(data);        alert(json.error);     } ... 

see this in http://api.jquery.com/jQuery.parseJSON/

if you still have the problem of slashes: search for security.magicquotes.disabling.php or: function.stripslashes.php

Note:

This answer here is for those who try to use $.ajax with the dataType property set to json and even that got the wrong response type. Defining the header('Content-type: application/json'); in the server may correct the problem, but if you are returning text/html or any other type, the $.ajax method should convert it to json. I make a test with older versions of jQuery and only after version 1.4.4 the $.ajax force to convert any content-type to the dataType passed. So if you have this problem, try to update your jQuery version.

like image 193
Guilherme Avatar answered Sep 28 '22 08:09

Guilherme


Firstly, it will help if you set the headers of your PHP to serve JSON:

header('Content-type: application/json'); 

Secondly, it will help to adjust your ajax call:

$.ajax({     url: "main.php",     type: "POST",     dataType: "json",     data: {"action": "loadall", "id": id},     success: function(data){         console.log(data);     },     error: function(error){          console.log("Error:");          console.log(error);     } }); 

If successful, the response you receieve should be picked up as true JSON and an object should be logged to console.

NOTE: If you want to pick up pure html, you might want to consider using another method to JSON, but I personally recommend using JSON and rendering it into html using templates (such as Handlebars js).

like image 43
sgb Avatar answered Sep 28 '22 10:09

sgb