i have this need.. I hope someone can give me a right advice !
i have to read the whole html content of a page using an ajax call, it is necessary that the client who is visiting my page is who that make this request to read the html content, and not my application (i mean using downloadstring method of c#)
After this, i need to read the response of the ajax call (in this case the html content of the page setted in the "url:" parameter of the ajax call) server-side (in my code-behind)
how i can do that? is possible?
Thank you for your help..
Stefano
Stefano,
You could get the html content through ajax using, for exemplo, jquery get like this:
$.get('ajax/test.html', function(data) {
//data is the html
});
After that you can make another ajax call sending the data to your "code-behind", as you can see in the complete code:
$.get('ajax/test.html', function(data) {
$.ajax({
dataType: "json",
data: "htmlData=" data
type: "POST",
url: '/code_behind.aspx',
success: function(response){
console.log(response);
}
});
});
I hope that this helps.
Related
Goal:
When you click on the row (1), new data shall display (3.) without the whole webpage will be updated/refreshed.
Problem:
1.
I need advice and I don't know where to find the funciton to display the picture number 2 and how to display the data and new object (3.) without update/refresh the whole webpage?
And
2
How do you create an icon to display loading picture?
Information:
- The page is based on ASP.mvc with C#
Use ajax functionality of either jquery or MVC ajax helpers.
You can find jquery ajax here.
and MVC ajax helper lib here
and here
you can make an ajax call to the server's websevice and it can return one of the well known web format (for e.g. json or XML). When the webservice call returns you can then "inject" the data in your html page with either javascript (dom manipulation) or using MVC helpers.
Here's one that could help..
http://www.asp.net/mvc/tutorials/older-versions/javascript/creating-a-mvc-3-application-with-razor-and-unobtrusive-javascript
You Can use jquery ajax which would call async action method(function). Data is returned the form of Json. You can write code to deserilize data and display it using jquery.
Create a Action method which would return JsonResult as viewresult as
public JsonResult GetJsonData()
{
return Json(new
{ testDataResult =TestDataResultObj.Data
JsonRequestBehavior
}, JsonRequestBehavior.AllowGet);
}
and write following jquery code:-
if (GetDataAsyc()) {
$.ajax({
type: "GET",
data: { testData: testDataResult },
url: url,// url of action method to be called asynch
dataType: "json",
cache: false,
traditional: true,
contentType: "application/json",
success: function (data) {
// on success assign testDataResult to messages //line
$("#MessagesLines").html(testDataResult .Html);
}
},
error: function () {
//Display error message
$("ErrorMsg").html("There was error whey trying to process your request")
}
});
}
Use ajax+PartialViews to update some page sections
My web service wants to know what is the page url of the caller.
I checked the HttpReq -> Url it is the actual web service Url. Also the HttpReq -> UrlReferrer but it is not right, neither.
Is it possible to find out the caller page url from server side at all? Or do I have to pass in the url via service dto?
Thanks for your help :)
If, for whatever reason, UrlReferrer doesn't work out for you then... the page where the request came from knows what its address is, right? Why not supply this data to the javascript that served the Ajax request, so you can send it up with the Ajax request?
Pass the page URL as a parameter to your web service by using the JavaScript document.location.href notation, like this:
$.ajax({
type: "POST",
url: "YourPage.aspx/DoSomething",
data: "{'pageUrl' : window.location.href}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
}
});
Then you can handle the pageUrl value in your service code.
In a previous post Handsontable Grid - loading and saving data from aspx web page I solved the "ajax data transfer to the server for Saving" with help from Stephen B. Burris Jr, for which many thanks. I have been unable to get the reverse process to work - that is the loading of data from the server to the grid. I am using sample code from GitHub for the client-side javascript code, but what I am not clear on at all is how the data needs to be "packaged" in response to the ajax call. I would be very greatful if someone could show me the structure of the code-behind method. Below is the javascript code with the ajax call:
var handsontable = $container.data('handsontable');
$(document).find('button[name=load]').click(function () {
$.ajax({
url: "Default.aspx/getJSData",
dataType: 'json',
type: 'GET',
success: function (res) {
handsontable.loadData(res.data);
$console.text('Data loaded');
},
error: function () {
$console.text('Load error');
}
});
});
Have a look at this post
It is a working solution using WCF.
You haven't stated what technologies it needs to be, but I noticed the C# tag.
I hope it helps.
Can anyone help? I am trying to interrogate the UrlReferer whcih should contain Google.com but it contains my current site. My web page is a standard HTM page and jquery calls a static method like so
[WebMethod]
public static void ProcessTracking(string jsonString)
Inside this method i do a standard lookup on Request.UrlReferrer like so
string referrerDomain = HttpContext.Current.Request.UrlReferrer.Host ;
But it always contains my current domain, this was a little suspect so i created a standard asp.net page and did the same and it works 100% without issue..
So it appears that when my htm page calls via jquery my webmethod (static) and interrogates the UrlReferrer it return ALWAYS my current site which is wrong.
Does anyone know a work around?
I even tried doing something in session_start etc in global.asax but no fix.
EDIT: How i am calling the page from jquery in html
$.ajax({
type: "POST",
url: "MyService.aspx/ProcessTracking",
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function(msg) {
},
error: function(msg) {
alert(error);
}
});
That script is hosted on your page, right? In that case, the referrer will be your site.
If you want the referrer for the page itself, then you need to pass it as an argument with your Ajax call; it won't be present in the HTTP header.
You can read the referrer of the page from the document.referrer property.
Surely it should contain your current domain, as that is webpage doing the post?
If you want to retrieve the original callers page, you'll need to store that in the original webpage, before calling your ajax code, and then passing that through.
Resources called via AJAX requests will consider the calling page as the referrer, which is why your domain is showing up as the referrer.
You had the right idea to use the Global.asax, but try hooking in to the BeginRequest method:
void Application_BeginRequest(Object Sender, EventArgs e)
{
string referrerDomain = HttpContext.Current.Request.UrlReferrer.Host ;
}
This is working as intended. When you use AJAX to post, you are sending a request from your page (your domain!) to the target server.
One workaround would be to store the original referrer's host name in a javascript variable when constructing your page:
var referrerHost = '<%= HttpContext.Current.Request.UrlReferrer.Host %>';
Then package that into the jsonData variable you're sending to the ProcessTracking method in the ajax function's data parameter.
We're redevloping a major section of our website and rather than use a 90k AJAX file I'd rather use a 19K jquery script.
I've seen the following articles;
Using jQuery for AJAX with ASP.NET
Webforms
jQuery autocomplete in ASP.NET webforms?
Autocomplete with ASP.Net MVC and
JQuery
The thing I don't get is how to do a postback to a specific method in either the code behind or another class.
I know in ASP.NET-MVC I can post back to a controller / action. How do I call a particular method in WebForms?
Something along the lines of; $.post("class and action", ( param:value}......
Any thoughts, code etc???
It is very easy to call specific methods in code-behind. Here is nice article with all the details by Dave.
Simply declare a method like this:
[WebMethod]
public static string GetDate()
{
return DateTime.Now.ToString();
}
This is all you need in jQuery:
$.ajax({
type: "POST",
url: "PageName.aspx/MethodName",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg, status, xhr) {
// Do something interesting here.
}
});
Caveats:
WebMethod must be on a static method
Must stringify posted data if sending anything (i.e. JSON.stringify(yourDataObject)), will be deserialized according to method parameters
msg is the response, the return result from your method is in the property msg.d