2

I am using jQuery's getJson to get a Json object and want to display it on html page.

I can display them successfully with code like this:

$.getJSON(full_name, {}, function(data) {

        $.each(data, function(index, field){
          $("div").append(index + " : " + field + "<br>");
        });
});

And in json data, there is an item with index called 'reference'. I want to change its field from a simple text to a hyperlink. And when I click this link, it will send a GET method request to an url, and display something get from the server on a new page.

How to change my page to achieve this function?

Thanks in advance.

1
  • where is the url specified Commented Aug 23, 2013 at 4:46

3 Answers 3

2

Try

$.getJSON(full_name, {}, function(data) {
    $.each(data, function(index, field){
        if(index == 'reference') {
            html = '<a href="link">' + field + "</a><br/>";
        } else {
            html = index + " : " + field + "<br>";
        }
        $("div").append(html);
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thx a lot tonight! You can assume the url is www.test.com/field. I want to send a GET request to this url and display the content I get in a new page.
@SeanZhu I assume you are not looking for an ajax request then html = '<a href="www.test.com/field">' + field + "</a><br/>"; will do
2
$.getJSON(full_name, {}, function(data) {

        $.each(data, function(index, field){
          if(index=='reference'){
          $("div").append('<a class="jMyLink">' + field + "</a><br/>");
          } else{
          $("div").append(index + " : " + field + "<br>");
          }
        });
});
$(".jMylink").on('click',function(){
  // Place your ajax call here
});

If you are not familiar with the use of ajax, you can refer it here

2 Comments

have a look at Arun P Jhony's method, He concatenates all the 'append' calls into a single string then appends. This would be a more efficient way. +1 since you took care of the ajax call.
@Sunny : .append() would be called only once even in my code, and the string is anyways concatenated before the append() call. But yes, using a variable would be more maintainable. Was just trying to focus on solving the core problem, so missed out. Thanks for pointing it out though :)
0
$.getJSON(full_name, {}, function(data) {

    $.each(data, function(index, field){
        if(index == 'reference'){
            $("div").append('<a href="link" class="sendrequest">' + field + "</a><br/>");
        } else {
            $("div").append(index + " : " + field + "<br>");
        }
    });
});

then to send request by anchor tag click

$(".sendrequest").on('click',function(){

// apply ajax here to send get request

 //send set state request
    $.ajax({
        type: "GET",
        url: "url name",
        data: {// your data
            },
        beforeSend: function () {
// handle before send request         
 },
        success: function (data, textStatus, XmlHttpRequest) {
// handle when request success        
},
        error: function (XMLHttpRequest, textStatus, errorThrown) {
// handle error
        }
    });


});

reference ajax

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.