Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8450917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:06:05+00:00 2026-06-10T11:06:05+00:00

I coded the following to send JSON data to the front end. But due

  • 0

I coded the following to send JSON data to the front end. But due to cross-domain security woes we need to convert it to JSONP, can someone suggest what I need to modify for this conversion?

Server side Code

JsonFactory jfactory = new JsonFactory();
         ObjectMapper mapper = new ObjectMapper();
         try {
                StringWriter stringWriter = new StringWriter();
                JsonGenerator jGenerator = jfactory.createJsonGenerator(stringWriter);
                jGenerator.useDefaultPrettyPrinter();
                jGenerator.writeStartObject(); // {
                jGenerator.writeStringField("title", title); // "title" : title
                jGenerator.writeStringField("Description", desc); // "desc" :
                jGenerator.writeFieldName("images");
                jGenerator.writeStartArray(); // [
                if(imageArray.size() != 0){
                for (String img : imageArray) {
                    jGenerator.writeString(img); // "msg 1"
                }
                }
                jGenerator.writeEndArray(); // ]
                jGenerator.writeEndObject(); // }
                jGenerator.close();
                response.getWriter().write(stringWriter.toString());
                System.out.println(stringWriter.toString());
                response.getWriter().close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("JasonGen servlet called end");

Front-end Javascript:

$.getJSON("http://localhost:8080/JsoupPrj/JasonGen?url="+ url[0],function(data){
                var imageArray=[];
                var imageOne = null;
                    imageArray=data.images;
....................
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:06:06+00:00Added an answer on June 10, 2026 at 11:06 am

    Since you’re using jQuery and you control the server-side, change your server code so that it looks for the callback parameter. The value of this parameter will be the name of the callback function that the client-side code is expecting. Then instead of returning the bare JSON, return it in the form: callbackName(rawJson).

    For instance, assuming a servlet API:

    String callbackName = request.getParameter("callback");
    
    // ... your JSON code 
    
    if (callbackName != null)
    {
        // JSONP wrapping:
        response.getWriter().write(callbackName +
                                   "(" + stringWriter.toString() + ")");
        System.out.println(callbackName + "(" + stringWriter.toString() + ")");
    }
    else
    {
        response.getWriter().write(stringWriter.toString());
        System.out.println(stringWriter.toString());
    }
    
    response.getWriter().close();
    

    Then on the client-side, continue to use jQuery.getJSON, but append a ?callback=? query parameter to your URL, or instead use jQuery.ajax for your call, and use the dataType: jsonp setting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code where i construct json data and send to the
I wanted to know if theres a way to send a JSON data along
I am trying to send some JSON data to my ASP.NET MVC3 controller action
Experts, The following function are successfully send data to the Controller.cs (server) var ajaxResponse
Hello i need to send some data back to the server and i want
I have the following code I am using to send data to a MailChimp
I am getting data from severs in json format but i want that user
The following code executes properly when the data key has no data to send,
I am trying to send data, in json format to my webservice, using POST.
I am trying to send a model as json. The model has binary data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.