Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
rated 0 times [  71] [ 6]  / answers: 1 / hits: 15618  / 10 Years ago, wed, july 23, 2014, 12:00:00

I've two HTML pages:

1) First HTML Page (page1.html):

<html lang=en>
<script src= type=text/javascript></script>
<script type=text/javascript>
$(document).ready(function () {

function json1(id,name){ = id; = name;

id_list = Array();
id_list.push(new json1(1,TEST_1);
id_list.push(new json1(2,TEST_2);
id_list.push(new json1(3,TEST_3);
id_list.push(new json1(4,TEST_4);
id_list.push(new json1(5,TEST_5);

id_list = JSON.stringify(id_list);

2) Second HTML Page (page2.html):

<html lang=en>
<meta http-equiv=Content-Type content=application/json; charset=utf-8 >
<script src= type=text/javascript></script>
<script type=text/javascript>
$(document).ready(function () {

url : '',
type : 'POST',
async: false,
contentType: application/json; charset=utf-8,
dataType: json,
success: function(data){
alert(Return OK);
error: function(xhr, ajaxOptions, thrownError){
alert('ERROR = ' + xhr.status + ' - ' + thrownError);

When I execute http://page2.html, .ajax returns me:
ERROR = 200 SyntaxError - Unexpected token <

When I change the dataType: json to text, the .ajax returns me all code HTML of page1.

I need to return the JSON created in page1.html.

Anybody can help me ?


ps: sorry about my English.

More From » jquery


First you have a mistake on the first html page

    id_list = Array();
id_list.push(new json1(1,TEST_1));
id_list.push(new json1(2,TEST_2));
id_list.push(new json1(3,TEST_3));
id_list.push(new json1(4,TEST_4));
id_list.push(new json1(5,TEST_5));

you forgot to add ) in the end of each push call

second thing is that when you are using ajax, you're asking the server, while in this case the server returns

<html lang=en>
<script ...

ajax function take this as result and doesn't execute any javascript code and doesn't wait until ready event

The result of the first html page that you want to be returned is


if you just put this json string in yout first html page witout any html tag, everything will work fine

I suggest you to put a server side code like php or nodejs to return the result you need in json not pure javascript because it's a client side langage

[#70084] Monday, July 21, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 641
Total Questions: 118
Total Answers: 101

Location: French Southern and Antarctic Lands
Member since Fri, Jan 6, 2023
1 Year ago
aubriea questions
Sun, Apr 26, 20, 00:00, 4 Years ago
Sun, May 26, 19, 00:00, 5 Years ago
Wed, Mar 27, 19, 00:00, 5 Years ago