We are using SAHI to do some benchmark tests. Currently they are collecting some times while exercising the application. These times are stored in a text file. We now have an app to collect and analyze this data. We have a restful API in .Net MVC 4. We created a javascript API thinking SAHI should be able to call the javascript methods and go on its merry way. We are having problems figuring out how to get SAHI to use JQuery properly and reference the API.
Javascript API Object:
;
var BenchmarkService = (function (options) {
var settings = $.extend({
serverName: "http://localhost"
}, options || {});
// public api
this.Benchmark = function (benchmarkName) {
var benchmark;
$.ajax({
type: "GET",
url: settings.serverName + '/BenchmarkServices/Services/Benchmark/' + benchmarkName,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (data) {
benchmark = data;
}
}
);
return benchmark;
};
this.SaveBenchmarkResult = function (benchmarkResultObject) {
var id;
var url = settings.serverName + '/BenchmarkServices/Services/SaveBenchmarkResult';
$.ajax({
type: "POST",
url: url,
data: JSON.stringify(benchmarkResultObject),
async: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) { id = data; }
});
return id;};
}
);
We would like be be able to call these methods within SAHI simply by referencing the .js file.
Is this possible?
Can you mention the Sahi version, your java version and os you are using. If you are a pro customer you can directly email to Sahi support team(after all you paid for the support)!
If you are using Sahi Os, let me know the version and I can look if it is possible or not.
Related
I'm sending a POST request to a Razor page handler using jQuery .ajax(). The network tab shows that this data is being sent as expected:
My breakpoints confirm that I'm hitting the handler, though the invitationId is not coming over (or at the very least, not deserializing correctly):
The JavaScript is as follows:
class GenericService {
constructor(controller) {
this.controller = controller;
}
async post(data, handler = "") {
return await $.ajax({
type: "post",
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN", $('input:hidden[name="__RequestVerificationToken"]').val());
},
data: JSON.stringify(data),
url: `/${this.controller}${handler}`,
contentType: "application/json",
dataType: "json"
});
}
}
(async () => {
const _ajax = new GenericService("Admin/Index");
await _ajax.post({ invitationId: 1 }, "Reset");
})();
I imagine that I'm misunderstanding the implicit deserialization, but examples I've seen elsewhere on the internet seem to indicate that sending a JSON serialized object should be fine.
This is using .NET Core 3.0.
Can anyone explain what might be my issue here?
As expected, I had some wires crossed as it relates to the deserialization.
If I send the data as a plain object without serialization (and remove the corresponding json type declarations from the .ajax() options), the invitationId comes across.
return await $.ajax({
type: "post",
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN", $('input:hidden[name="__RequestVerificationToken"]').val());
},
data: data, //Remove JSON.stringify(...)
url: `/${this.controller}${handler}`
//contentType: "application/json", Remove
//dataType: "json" Remove
});
In a big web application, is there any advantage(s) or disadvantage(s) of using a common javascript http_get method in a common .js file like the example below over writing each of them seperately ?
function http_get(url, async) {
var post = $.ajax({
type: "GET",
url: url,
async: async,
dataType: "json",
contentType: "application/json"
});
return post;
}
Thanks in advance.
I have an older JAX-RS application that I'm running on IBM Liberty profile 16.0.0.4 with the jaxrs-2.0 feature. I'm getting some weird behavior that I can't explain, and need some help.
JAX-RS service code:
#Path("/loadData")
#POST
#Produces("text/plain")
public String loadData(#Context HttpServletRequest request) {
String id = request.getParameter("id");
String email = request.getParameter("email");
// add'l request processing code
}
JQuery code:
$(document).ready(function() {
var reqData = {id:"123456", email:"user#email.com"};
$.ajax({
type: "POST",
cache: false,
url: "http://localhost:9080/jaxrs/loadData",
data: reqData,
contentType: "application/json",
dataType: "json",
timeout: "30",
beforeSend: function() { ... },
success: function(data) { .... },
complete: function(data) { .... }
});
});
JQuery (in IE) Results:
request.getParameter("id") or ("email") = null;
request.toString() = org.apache.cxf.jaxrs.impl.HttpServletRequestFilter#f272c8dc
IOUtils.toString(request.getInputStream) = "id=123456&email=user1%40email.com"
SoapUI Results:
request.getParameter("id") = 123456 and ("email") = user#email.com;
request.toString() = org.apache.cxf.jaxrs.impl.HttpServletRequestFilter#de62638a
IOUtils.toString(request.getInputStream) = ""
I compared the RAW HTTP request in SoapUI and in IE's console, they both appears to be the same, as far as I can tell. So that's really got me confused, both JQuery and SoapUI are performing POST, but seems in IE's case the query stream is not being parsed into parameters, rather just maintained as a string. I've tried to mess around with the contentType and request declaration with no effects. I was originally using JQuery 1.7.1, but I tried on 3.1.1 with no effects. Has anyone seen this before. Any help or insights would be really great, thanks!
Try using "JSON.strigify" for params
$(document).ready(function() {
var reqData = {id:"123456", email:"user#email.com"};
$.ajax({
type: "POST",
cache: false,
url: "http://localhost:9080/jaxrs/loadData",
data: JSON.stringify(reqData),
contentType: "application/json",
dataType: "json",
timeout: "30",
beforeSend: function() { ... },
success: function(data) { .... },
complete: function(data) { .... }
});
});
I have a function in my javascript that returns a json. I want to get the return value of that javascript function using c# so I can convert it in dataTable. Can anyone help me? Sorry I'm just a beginner in asp.net webforms.
JS
function getAllMessages()
{
$.ajax({
url: '/api/Messages/',
contentType: 'application/html ; charset:utf-8',
type: 'GET',
dataType: 'JSON'
}).success(function (result) {
});
}
since you don't have the actual code , i'll just give an algorithm. I experienced this scenario in one of my projects.
In your html/asp.net button, execute a webmethod that has a parameter to receive the json format
$.ajax({
type: 'POST',
url: 'YourPage.aspx/c#MethodThatWillReceiveJSON',
data: "{c#MethodParameterName:" + JSON.stringify(YourJSONvalues) + "}",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (r) {
alert(r.d.exampleDataFromJson);
}
});
You need to create another javascript function that will call your original javascript (The one who will return the json format) and use that function to pass it as an argument to the parameter of your c# webmethod.
//your original javascript function that returns json
function getJsonData()
{
// your code
}
// use that js function to store in a var , then pass it to your c#
var MyJSONdata = getJSONData();
$.ajax({
type: 'POST',
url: 'YourPage.aspx/c#MethodThatWillReceiveJSON',
data: "{c#MethodParameterName:" + JSON.stringify(MyJSONdata) + "}",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (r) {
alert(r.d.exampleDataFromJson);
}
});
If you use a String type as a parameter in your c# webmethod, you can not use/manipulate it as a json stuff, you need to convert your String variable using json serializer :) which you can find a lot of reference here in net/stackoverflow, it is just one liner of code to convert. :)
Can someone please put some light on how to connect your Cordova App to WebServices hosted on a server.
Method: POST method
URL: http://192.168.1.20:3030/Service1.svc
Service Method: Login
Here's what I have been trying(Username-Password verification):
function GetLogin() {
var UName = "12345678";
var UPassword = "12345678";
var jData = { UserName: UName, Password: UPassword };
alert('1');
$.ajax({
type: "POST",
url: "http://192.168.1.20:3030/Service1.svc/AmalatLogin",
data: JSON.stringify(jData),
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
processdata: true,
success: function (msg) {
alert("Start");
document.getElementById("demo").innerHTML=msg.ResponseMessage.toString();
alert("End");
}
});
}
The above code is the javascript part. I have simply called the onClick="GetLogin()" on the submit button.
The problem is the code runs when the html file is being open using localhost( i.e. "http : //(localhost)/ aaa.html" (without spaces) or even by using Google Postman), but fails to do so when I run it from the directory(as in C:\www\aaa.html).
I have been searching for the solution everywhere. Please put some light on whether this is the limitation of technology or do I need to add some plugins.
Basically alert('1') is showing, other alerts are not.
They are all in same network