0

I have a servlet that contains a string, wherein the latter is a JSON string with multiple entries. Now I want to access this String with ajax through jQuery. Now here if my function:

function myFunction() {
    $("#myButton").click(function() {
         $.ajax({ 
             url: // servlet,
             type: "GET",
             dataType : "text",
             success: function() // I want to display the string from the servlet,
             error:  // stuff


    // other code

Anyway how can I do this. Also can I place another function in the success part rather than an anonymous function?

5 Answers5

0

I want to display the string from the servlet,

your success method has a parameter data

success: function(data){ alert( data ); } 

Also can I place another function in the success part rather than an anonymous function?

success: namedFunction, 

function namedFunction(data){ alert(data); }
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • `namedFunction(data)` will be called/executed when that line of code will be parsed. Function expression need to be passed I guess. `data` will be first argument of the callback function hence no need to pass `data` just in case you are about to write `namedFunction(data)` inside anonymous function.. – Rayon Jan 04 '16 at 10:06
0

You can pass function expression to be executed on success or error. First argument for success callback is response sent via server.

Try this:

function successGoesHere(response) {
  console.log(response);
}

function errorGoesHere(err) {
  console.log(err);
}

function myFunction() {
  $("#myButton").click(function() {
    $.ajax({
      url: "YOUR_URL",
      type: "GET",
      dataType: "text",
      success: successGoesHere,
      error: errorGoesHere
    })
  })
}

As you are passing dataType : "text" in ajax config, response will always be plain text, just in case you are expecting it to be json, your response will be string representation of json hence you will have to parse it using JSON.parse(response) to get JSON object

Rayon
  • 36,219
  • 4
  • 49
  • 76
0

Is this what you're looking for?

function getString(url, handleResponse, handleFailure) {
    var request = new XMLHttpRequest();
    request.open("GET", url, true);
    request.onload = function () {
        if (request.status >= 200 && request.status < 400) {
            if (handleResponse) {
                handleResponse(request.responseText);
            }
        } else {
            if (handleFailure) {
                handleFailure(request.status);
            }
        }
    };
}

this just gets a string from URL (No JQuery)

guysigner
  • 2,822
  • 1
  • 19
  • 23
0

Use ajax request success : function(resp){ code for parsing json and show parsed json // var json = $.parseJSON(data); }

You need to look this

Community
  • 1
  • 1
Rahul Bhawar
  • 450
  • 7
  • 17
0
var callback = function(data, textStatus, xhr)
{
    alert(data + "\t" + textStatus);
}

var testman = function(str, cb) {
    var data = 'Input values';
    $.ajax({
        type: 'post',
        url: '',// servlet
        data: data,
    success: cb
    });
}
testman('Hello, Dear', callback);
Manoj Saini
  • 339
  • 2
  • 8