I am Using web service in with Jquery Ajax this returning me the following string
{"d":"[{\"username\":\"ABC\",\"designation\":\"\"}]"}
but when I am trying to parse this it is giving me error
var response = '{"d":"[{\"username\":\"ABC\",\"designation\":\"\"}]"}';
console.log(JSON.parse(response));
As others have noted, you need to remove the quotes around the braces.
const PATTERNS = [/"(\[)/g, /(\])"/g]; // Invalid patterns
const JsonFixer = json => PATTERNS.reduce((s, re) => s.replace(re, '$1'), json);
var rawJsonResponse = '{"d":"[{\"username\":\"ABC\",\"designation\":\"\"}]"}';
console.log(JSON.parse(JsonFixer(rawJsonResponse)));
.as-console-wrapper { top: 0; max-height: 100% !important; }
Related
Goal :
1. Extract csrf value from my GET request response body
2. Store extracted 'csrf' value in environment variable
3. Use it in subsequent POST request.
Found a Solution and working:
var matchSETTINGS cheerio.load(responseBody);
Extract var matchSETTINGS = text.match(var a= (.*););
This is bit complicated, but achievable using various ways. Here I'm showing you a static way, change it as per your requirement.
Postman supports cheerio library, that you can use to parse HTML responses.
var html = cheerio(responseBody);
//get script tag data
var htmlData = html[14].children[1].children[5].children[0].data;
var csrfIndex = htmlData.search(/'csrf'/i);
var dataBeforeCsrf = htmlData.slice(0, csrfIndex);
//remove content before csrf node
htmlData = htmlData.replace(dataBeforeCsrf, '');
//to make it in right format of JSON, replace single quote with double,
//remove ; and add bracket
htmlData = htmlData.replace(/'/g, '"');
htmlData = htmlData.replace(';', '');
//parse to JSON
var jsonData = JSON.parse('{' + htmlData);
//print csrf
console.log(jsonData.csrf);
I am trying to make an app in Android and have some problem. I have searched the web but I haven't found an obvious solution.
In my Android app I send a Post request for a login task using Retrofit.
#FormUrlEncoded
#POST("/login")
Call<Boolean> loginUser(#Field("username") String userName, #Field("password") String password);
My server is in Node.js with express and I don't know how to extract the username and password parameters.
my app.js file looks like this:
var http = require('http');
var express = require('express');
var bodyparser = require('body-parser');
var connection = require('./dbConnection');
var app = express();
require('./models')(app);
require('./controllers')(app);
require('./routes')(app);
app.use(bodyparser.urlencoded({extended: true}));
app.use(bodyparser.json());
connection.init();
var server = app.listen(3000, function() {
console.log('Server listening on port ' + server.address().port);
});
And my routes file for login looks like this:
var loginM = require('../models/login');
var loginC = require('../controllers/login');
module.exports = function(app) {
app.post('/login/', function(req, res, next) {
loginM.attemptLogin(req.body, res);
});
}
req.body does not seem to give me the #Field variables, I have tried req.headers and req.params as well with no success. Can someone explain how to extract them?
Much appreciated
On NodeJs side, or better: expressjs app side, you need to use a body-parser middleware, such as https://github.com/expressjs/body-parser
npm install body-parser --save
Since body-parser supports JSON as well as URL encoded input (which retrofit #Field generates) you need to add appropriate middleware functions to app:
var bodyParser = require('body-parser');
// parse JSON inputs
app.use(bodyParser.json());
// Also, parse URL encoded inputs
app.use(bodyParser.urlencoded());
// handle requests
app.post('/login/', function(req, res, next) {
loginM.attemptLogin(req.body, res);
});
Also, remember that you add body-parser middleware Before adding routes/controllers to the app. Because parser middleware should be executed before so that input is parsed by the time request handling logic is executed.
var app = express();
// first
app.use(bodyparser.urlencoded({extended: true}));
app.use(bodyparser.json());
// after
require('./models')(app);
require('./controllers')(app);
require('./routes')(app);
I want to retrieve a value of a javascript section in a PHP file with my application in Java.
The PHP page contain something like :
<script type="text/javascript">
var tab= new Array();
tab[0] = "value0";
tab[1] = "value1";
tab[2] = "value2";
</script>
I'm using jsoup for parsing the HTML tag. I tried to use Rhino but I don't find example.
Context context = Context.enter();
Scriptable scope = context.initStandardObjects();
Object result = null;
Reader reader = new InputStreamReader(inputStreamOfThePage);
result = context.evaluateReader(scope, reader, "page", 1 , null );
Scriptable varValue = (Scriptable)scope.get("tab", scope);
String valueStr = (String)varValue .get("tab[0]", varValue );
It's giving me the exception :
java.lang.ClassCastException: org.mozilla.javascript.UniqueTag cannot be cast to org.mozilla.javascript.Scriptable
I don't know how to cast the object. Maybe there is a better way to do what I want.
Thanks
Jsoup is not suitable to parse Javascript... It's normal it doesn't work !
I am making a simple html servlet program where I need get JSON object from the servlet, and in html I am able to get the data, but how can i refer to each attribute?
Here is the servlet get method
PrintWriter out=response.getWriter();
StringBuffer emps = new StringBuffer("{employees:[");
emps.append("{fullname:\"Abhishek Raj Simon\"},{email:\"a#a.com\"}]");
emps.append("}");
out.println(emps);
JS to send
function getJson()
{
var url_action="/temp/jsontest";
var form=document.forms["mainForm"];
var splitOutput;
var client;
var dataString;
if (window.XMLHttpRequest){
client=new XMLHttpRequest();
} else {
client=new ActiveXObject("Microsoft.XMLHTTP");
}
client.onreadystatechange=function(){
if(client.readyState==4&&client.status==200)
{
var res=client.responseText;
alert(res);
alert(client.responseText.employees.fullname); //DOES NOT WORK
alert(client.responseText.employees.email); //DOES NOT WORK
}
};
client.open("GET",url_action,true);
client.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
client.send();
and a simple form
<form>
<input type="button" value="get me some json" onclick="getJson();">
</form>
When i click on the button, i get only 1 alert displaying {employees:[{fullname:"Abhishek Raj Simon"},{email:"a#a.com"}]} How can i fetch Abhishek Raj Simon and a#a.com using fullname and email respectively?
Edited after reading post from Artem
my servlet
Gson gson = new Gson( );
List<Employee> employees = new ArrayList<Employee>();
Employee emp=new Employee();
emp.setFullname("Abhishek Raj Simon");
emp.setEmail("a#a.com");
employees.add(emp);
response.setContentType( "application/json");
out.println( gson.toJson( employees));
js part
var res=eval('(' + client.responseText + ')');
alert(res);
alert(res.employees.fullname);
alert(res.employees.email);
I think you should slightly change the JSON that you send form the servlet: {employees:[{fullname:"Abhishek Raj Simon", email:"a#a.com"}]} would work a bit better in that context.
I'd recommend jQuery as pap has also advised. The code would be:
$.getJSON('/temp/jsontest', function(data) {
var items = [];
for (employee in data.employees) {
items.push(employee.fullname + '<' + employee.email + '>');
}
alert('Employees: ' + items.join(', '));
});
In my opinion it is lot simpler and easier to understand than dealing with raw XHR. The jQuery $.getJSON will do GET for you, and then evaluate the JSON response so the function is presented with nice JSON representation of your data that is easy to manipulate.
EDIT:
After interesting discussion here is some more improvement you could introduce in order to replace the low-level code with proper JQuery-based implementation.
<script>
$(document).ready(function() {
$("#json-button").click(function() {
$.getJSON('/temp/jsontest', function(data) {
var items = [];
for (employee in data.employees) {
items.push(employee.fullname + '<' + employee.email + '>');
}
alert('Employees: ' + items.join(', '));
});
});
});
</script>
<form>
<input id="json-button" type="button" value="get me some json">
</form>
I would suggest you to use GSON library, which enables you to serialize Java object to json, to avoid writing it by yourself. If you do not want to use GSON, there are plenty of others libraries which uses same capabilities.
//inside your get method
Gson gson = new Gson( );
List<Employe> employees = new ArrayList<Employe>( );
// populate your list here
response.setContentType( "application/json");
response.getWriter( ).println( gson.toJson( employees));
//end
then in javascript you can do as it's already suggested in other answers here. And do pay attention to update response content type.
var res=client.responseText;
var temp = 'resObj=' + res;
eval(temp);
alert(resObj.employees.fullname);
JSON is just text, but in javascript syntax. You need to pass it through the "eval" method that will evaluate and execute the text as javascript.
My advice, though, is to use jQuery or some other javascript framework to avoid having to mess with all that boiler-plate javascript.
It's because you are receiving the JSON String but you're not converting it into a JSON Object. There's a eval() function that evaluates your JSON String and returns a JSON Object.
The following example should work (though untested).
if(client.readyState==4&&client.status==200)
{
var res=eval('(' + client.responseText; + ')');
alert(res);
alert(res.employees[0].fullname);
alert(res.employees[0].email);
}
WARNING: I suggest reading the security concerns when using eval(). Alternatively, go to JSON.org and download a Javascript JSON parser or use JQuery instead.
looking for a javascript class like swfobject to embed java and have a simple fallback if the user doesn't have java or refuses the security prompt.
thanks,
Josh
You could build one pretty easily.
Have something like a div set up like this:
<div id="java-applet">
Message to user saying that they need Java here
</div>
Then add Java Plugin Detection (builder) to your JavaScript. Then if that returns true, then do something like:
document.getElementById("java-applet").innerHTML = "<applet>stuff here</applet>";
appletobject may work, but I have not used it.
Just embed the applet like you normally do and insert the fallback inside or insert a javascript snippet to remove the object: Besides param, you can add other elements, e.g. paragraphs with text or javascript calling some function to replace the object.
<script type="text/javascript">
function replace_object(x) {
$(x)...
}
</script>
<object x="y" id="some_applet">
<param name="y" value="z">
<p>java not available. some alternative here. <!-- option 1 --></p>
<script type="text/javascript">
replace_object('some_applet'); // option 2
</script>
</object>
This helps!
I got a very strange problem while using applet to do batch file downloading from the server side.
The Ajax request seems conflict with applet request, the applet file downloading interrupted with some socket exception.
The applet works fine under JRE5.0, it might be caused by our recent upgrade to JRE6.0.
<div id="java-applet"></div>
<script>
var t;
function startApplet() {
var attributes = {codebase:'<%=request.getContextPath()%>',
code:'<%=appletClass%>',
archive:'applet/SignedApplet.jar',
width:0,
height:0} ;
var parameters = {para1:'value1',
para2:'value2',
java_arguments:'-Xms64m -Xmx512m'
} ;
var version = '1.6' ;
var buildAppletTag = function() {
var tag = '<applet';
for (var attribute in attributes){
tag += (' ' + attribute + '="' + attributes[attribute] + '"');
}
tag += ">";
for (var parameter in parameters){
tag += '<param name="' + parameter + '" value="' + parameters[parameter] + '"/>';
}
tag += '</applet>';
return tag;
};
document.getElementById("java-applet").innerHTML = buildAppletTag(attributes, parameters, version);
clearTimeout(t);
}
t = setTimeout("startApplet()", 1000); // delayed
</script>