$.get implementation based on jquery returns parsing JSON data and Jsonp Cross-domain

Source: Internet
Author: User
Tags eval php code string format

Straight since all is written Ajax is the use of $.getjson this method, the main figure is cross-domain convenient, of course, cross-domain sometimes will bring unsafe hidden dangers, now the project is $get, the example is basically return string format, write their own getjson write habits, Want to return to the JSON format. Here are three ways to implement JSON-formatted returns.

JSON and Eval method implementations

JS Code:

$.get ("Index.php?a=manage/kcinfo&act=show", {SPBS:SPBS},

function (data) {

var d = eval ("+ Data +"));

var html = ';

if (D.flag = = 100) {

for (i = 0, ln = d.data.length i < ln; i++) {

html = ' <tr><td> ' +d.data[i]. Xdate+ ' </td><td> ' +d.data[i]. spbs+ ' </td><td> ' +d.data[i]. salenum+ ' </td><td> ' +d.data[i]. freqnum+ ' </td></tr> ';

}

$ (' #showinfo '). Nextall (' tr '). Remove ();

$ (' #showinfo '). After (HTML);

} else {

Alert ("Failed to modify!") ");

}

return false;

})

PHP Code:

$res [' flag ']= ' 100];

$res [' Data ']= $data;

Exit (Json_encode ($res));

Related skills

The Get method does not specify a format for the returned content. var d = eval ("+ Data +") is used here; Converts an object to a JSON object.

For a JSON string returned by the server, if the jquery asynchronous request does not have a type description, or is accepted as a string, it is necessary to do an object-by-case, either in a cumbersome way, or to put the string in eval () once. This is also a good way to get JSON objects in a normal Javascipt way, as illustrated in the following example:

The code is as follows Copy Code
var dataobj=eval ("(" +data+ ")");//Convert to JSON object

Why do I have to eval here to add "(" ("+data+"));/"?

The reason is that eval itself is a problem. Since JSON starts and ends with "{}", in JS it is treated as a block of statements, so it must be coerced into an expression.

The purpose of parentheses is to force the Eval function to force an expression in parentheses (expression) into an object when processing JavaScript code, instead of executing it as a statement (statement)

2.for loops can be written for this for (i = 0, ln = d.data.length i < ln; i++), the first look will certainly say the principle of circular optimization is not to write the calculation in the circulation body, this will result in multi-layer operation, inefficient. But everyone note: i = 0, ln = d.data.length This part is before the semicolon, belongs to the initialization value, that is, in the loop only once, syntax (not only JS can these, PHP for can also write):

for (variable = start value; variable <= end value; variable = variable + step value) {Code to execute}

Get specify JSON format return

JS Code:

$.get ("Index.php?a=manage/kcinfo&act=show", {SPBS:SPBS},

function (d) {

var d = eval ("+ Data +"));

var html = ';

if (D.flag = = 100) {

for (i = 0, ln = d.data.length i < ln; i++) {

html = ' <tr><td> ' +d.data[i]. Xdate+ ' </td><td> ' +d.data[i]. spbs+ ' </td><td> ' +d.data[i]. salenum+ ' </td><td> ' +d.data[i]. freqnum+ ' </td></tr> ';

}

$ (' #showinfo '). Nextall (' tr '). Remove ();

). After (HTML);

11

} else {

12

Alert ("Failed to modify!") ");

13

}

14

return false;

15

}, "JSON")

The PHP code is the same as the previous one.

Related Knowledge points:

This is "JSON" (must have double quotes) is not JSON, I always write json (without double quotes), cause the return has failed, next time everyone attention

Get-Specify JSONP implementation cross-domain

JS Code:

View Source

Print?

1

$.get ("Index.php?a=manage/kcinfo&act=show", {SPBS:SPBS},

2

function (d) {

3

var d = eval ("+ Data +"));

4

var html = ';

5

if (D.flag = = 100) {

6

for (i = 0, ln = d.data.length i < ln; i++) {

7

html = ' <tr><td> ' +d.data[i]. Xdate+ ' </td><td> ' +d.data[i]. spbs+ ' </td><td> ' +d.data[i]. salenum+ ' </td><td> ' +d.data[i]. freqnum+ ' </td></tr> ';

8

}

9

$ (' #showinfo '). Nextall (' tr '). Remove ();

10

$ (' #showinfo '). After (HTML);

11

} else {

12

Alert ("Failed to modify!") ");

13

}

14

return false;

15

}, "Jsonp")

PHP Code:

View Source

Print?

1

Exit ($_get[' callback '). ' ('. Json_encode ($res). ");

Related knowledge

Note that URL requests in JSONP format are more than: callback parameters and _ request parameters

If you use Getjson, you immediately think of this as a Cross-domain prerequisite callback parameter callback

Only need exit ($_get[' callback '). ' ('. Json_encode ($res). "); This format returns you to a JSON cross-domain.

Extended Knowledge:

The difference between JSON and JSONP:

Ajax: The most respected or preferred option is to use JSON to pass the data, relying on JSONP to cross the domain

JSON and JSONP have only one letter difference, but they're not really the same thing at all: JSON is a data interchange format, and Jsonp is an unofficial Cross-domain data interaction protocol that relies on the ingenuity of developers. We take the recent fire spy war film For example, JSON is used to write and exchange information of the "Code", and Jsonp is the use of code to write the information to his comrades used in the joint approach. Did you see that? One is the format of describing information, and the other is the method of communication between two parties.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.