I have created a WebMethod (dt) in code behind page c# of default.aspx page, i want to display response data(json string) on another page like default2.aspx page and bind json response data using ng-repeat angularjs
[WebMethod]
public static string getJson()
{
string data = string.Empty;
data = (string)HttpContext.Current.Session["getData"];
return data;
}
WebMethod defined in default.aspx.cs,
I want to bind json data(getting from webmethod) using ng-repeat angularjs in default2.aspx page from button click on default.aspx page. I am getting json string on same page, but don't know that how to show this on another page.
Any suggestion or help....
Just use something like the follwing on your second page i.e Default2.aspx
$.ajax({
url: 'Default.aspx/getJson',
.
.
});
Related
Is it possible to retrieve query string data on page load using javascript?
Let me explain my sample project in steps::
I have a view page showing tabular data. On button press, it retrieves id of the row and calls javascript function.
This javascript function, getDetails(id) has to call another view page, say Application.cshtml.
I have to pass the value of id and a variable message to this view page Application.cshtml.
I am trying to pass this as query string.
Now in this view page Application.cshtml, I have to retrieve the value of id and call contoller method to show the details of the id on page load.
Is it possible to do using javascript?
function getDetails(id)
{
var message = "testing";
var id_ = id;
window.location = "/FirmModels/Application/" + id_;
}
My problem is how can I retrieve the value of id and call controller method on page load using javascript in Application.cshtml?
Your question is a bit unclear but as far as I understood this question you can do this in two ways.
Assuming that you have something like this in your JS file.
window.location = "/Controller/Application?id= " + id_;
In the first method, the View inside your Controller will look like this
public ActionResult Application()
{
string id= Request.QueryString["id"];
//Do your operations here
return View();
}
The second way to do this to pass the id as a mandatory parameter to the view.
public ActionResult Application(string id)
{
return View();
}
I have in the database table just one column and it's content either javascript link like
<script src="some src"></script>
or javascript code like
<script>
some code
<script>
So my question is it possible to correct render that javascript inside cshtml file?
You can use the Html.Raw method in your view
#model YourViewModel
#Html.Raw(Model.YourScriptContent)
Assuming YourScriptContent is the property of your view model on which you are setting the script content value, in your GET action.
public ActionResult Index()
{
var vm = new YourViewModel();
vm.YourScriptContent = "<script>alert('testing');</script>";
return View(vm);
}
By default, razor will HTML encode the return value of any c# code/expression with the prefix #. But the Raw method will return the markup which is not HTML encoded, which is what you need in this case.
Since you are not HTML encoding when you render it, I am hoping you completely trust the script content values :). Read about script injection attacks if you do not follow the previous statement.
I have a jersey client class which is making a put request to a rest.
//JerseyClient
public void putRequest() throws Exception{
reloadUri();
Response response = target.request(MediaType.APPLICATION_JSON)
.accept("application/json;charset=UTF-8")
.header("Content-Type", "application/json")
.header("Authorization", "Basic OTA1MzAwNjY3MDg2OjZ4dDg5dk50VXdCbg==")
.put(Entity.entity(sub, MediaType.APPLICATION_JSON),Response.class);
System.out.println(response);
if(response.getStatus() == 200) {
System.out.println("put request using Json is Success");
}
}
It's working fine, but I wanted to call this function with a button click. And this button is placed on one of my jsp file. So, is there a way to call this request inside that jsp file when button clicked.And redirect the page into new jsp according to response of rest.
No directly by the jsp. If you include this code in the jsp, it will be executed while parsing the jsp page in the server.
You should include it in a Java component which will be called by a Servlet (or controller, filter, etc...). The button in your jsp will make a call to the URL which executes that component, and is there where you must do whatever you need with the result.
If you want to execute directly in the jsp while clicking the button, you must make it using javascript in client side.
You may call this method using URL reference:
define a path annotation for method:
#Path("/PATH")
#GET// or #POST
public void putRequest() throws Exception{
...
}
And then, from you jsp page you can define the button to redirect to this URL to initiate the above method.
You may read more about jersey annotations here.
I have this link on my test.jsp page
<a onclick="treeViewAjax('${searchDummyUrl}/view/${search.DummyNumber}/1')">View</a>
Now when I click on this then treeViewAjax ,A JavaScript method is being invoked as you can see in the linkHere is the treeViewAjax method
function treeViewAjax(Url){
$.ajax(Url, function(data) {
alert(data);
});
}
and at the same time my Spring controller's searchDummyView method invoked
#RequestMapping(value = "/Dummy/searchDummy/view/{dummyNumber}/{dummyTypeId}", method = RequestMethod.POST)
public #ResponseBody
List<Report> searchDummyView(ModelMap modelMap, #PathVariable("dummyNumber") Integer dummyNumber, #PathVariable("dummyTypeId") Integer dummyTypeId) {
List<Report> reportList = new ArrayList<>();
reportList.add(dummyService.readReport(dummyNumber, dummyTypeId));
//modelMap.addAttribute("reportList", reportList);
return reportList;
}
Now when as Spring expert's can understand that I have used #ResponseBody annotation as to make to ajaxical request So it then again send response back to the requested URL.Now here again control is on my JS method treeViewAjax and when I alert the data then it show the list values perfectly. Now I am stuck here that How can I capture reportList returned by searchDummyView method on JSP page as well as How to iterate/Show its value on the JSP page using EL. Any suggestions? Note: I have tried to show reportList like this but it didn't work for me
<c:choose>
<c:when test="${reportList.size() > 0}">
<c:forEach items="${reportList}" var="list">
//iterations over list but
</c:forEach>
...
... />
It did't display anything from list because of my condition < 0 and I guess it is returning 0 size here OR not accessible here.(could be any reason)
This code will not work in your case :
<c:choose>
<c:when test="${reportList.size() > 0}">
<c:forEach items="${reportList}" var="list">
//iterations over list but
</c:forEach>
...
... />
Your JSTL and EL will be processed by the server and sent to the browser . But in your case the JSP is already rendered in the browser and you are firing a AJAX request which will give back some data . You are getting an AJAX response from server , not a full fledged response which the browser will try to render . To create a table with the data of AJAX response I guess there are two options :
Build the table using HTML and javascript dynamically once you get the AJAX response back.
Have a <div> inside your JSP and load another JSP with the table in that AJAX success.
I'd like to create my own JS widget, which it must be dinamic.
I mean, for example, the html generated from the downloaded script :
<script src="www.mywebsite.it/widget/?ID=2&Category=4" type="text/javascript"></script>
must be different from :
<script src="www.mywebsite.it/widget/?ID=1&Category=5" type="text/javascript"></script>
and the Data into HTML should be taken from Database, on my server. So, I need to call an aspx page that create javascript that will create html? Or which could be the solution?
The better way is to use generic handler with .ashx, if you want retrieve data from server and send data in JSON or XML.
Next, the data will be inserted in page with javascript.
So, if I understand well, you do generate an .aspx that contains your template and a javascript that hold the code to navigate in Category as this if you use JQuery :
$.ajax({
url: 'data.ashx?ID=2&Category=5',
success: function(data) {
$('.result').html(data);
alert('Load was performed.');
}
});
Server behind (ashx) :
private readonly JavaScriptSerializer _js = new JavaScriptSerializer();
public void ProcessRequest(HttpContext context)
{
//Do logic and retrieve data here
Categorys c = GetFooById(context.Request["id"]);
context.Response.Write(_js.Serialize(c));
context.Response.ContentType = "application/json";
}
It seems that you'd want to use AJAX.
The script source shouldn't be dynamic (it can't be cached if it is), but the script itself could call whatever page you like to pull back data (say in JSON format) or raw markup to place in a pre-defined element.
Don't use the ASPX page to create javascript if you can help it.
Consider using a JavaScript library such as jQuery to help you.