Use Server.MapPath in jQuery AJAX call while deployed on local IIS - javascript

I am using WebMethod on my ASPXpage and I call it from jQuery on the same page like this:
$.ajax({
type: "POST",
url: "WebForm1.aspx/Saveuser",
contentType: "application/json; charset=utf-8",
data: parameters,
dataType: "json",
success: AjaxSucceeded,
error: AjaxFailed
});
This works fine in my debug environment, but when I deploy on the hosting site it does not. The problem seems to be in the URL because on the server the path would be different. So I used Server.MapPath in various ways but none of them worked.
Url: '<%= Server.MapPath("~/MyPage.aspx/GetSomeData")%>'
Can anyone overcome this defect?

Related

IIS URL Rewrite and ajax calls in Asp.NET Core 2.2

I have a Asp.NET Core 2.2 site at localhost:81 and I have set up a IIS URL Rewrite to make it accessible from
www.mydomain.com/mysite
Note: I have other sites located on other ports on localhost which are published the same way.
The URL Rewriter takes care of rewriting the references in .cshtml files, but it doesn't handle my ajax calls:
$.ajax({
type: "GET",
url: "/Customers/Index?handler=HairColor&customerID=#Model.CustomerID&date=" + myDate,
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
data: null,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {},
failure: function(response) {}
});
I don't know how the rewrite module could ever know that it should rewrite that specific url in my javascript.
Question:
What is the right way to make the ajax call compatible with my website setup?
I am starting to wonder if hosting multiple sites like this on a single domain
is the wrong approach?

WebService in JqueryMobile(Response in Json)

I'm doing a project in Jquery Mobiles and need an webservice. Normally in android we have the http connection and we got the response as xml/json etc.. We parse the data and processed to the view. Here,In case of jquery , facing problem to solve the issue.. I read that ajax call is using for the webservices.
Sample code
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "some url",
data: "{}",
dataType: "json",
success: function (msg) {
alert(msg);
//$("#resultLog").html(msg);
}
});
Also,I refer the google api_Place Search, the service for this is entirely different
RefernceLink Click HERE!
Does anyone suggest the proper way to implement the webservice to interact with my server and retrieve the json response?
Thanks in Advance!

simple REST call from Javascript

I am trying to call my service from here
http://diningphilospher.azurewebsites.net/api/dining
using below javascript,
$.ajax(
{
type: "GET",
dataType: "json",
url: "http://diningphilospher.azurewebsites.net/api/dining/",
success: function (data)
{
alert(data);
}
});
But I am getting error relating cross origin. I see people suggest using JSONP but I guess my server does not support JSONP. I studied CORS and could not understand the head or tail of it. I would like to know the way around to read the JSON that sits in different domain.
I hope this should work:
$.ajax(
{
type: "GET",
dataType: "jsonp",
url: "http://diningphilospher.azurewebsites.net/api/dining/",
success: function (data)
{
alert(data);
}
});
Or just add/append ?callback=? along with your cross-domain url.

MVC Deployment - Relative Path issue with respect to Application Path

While deploying my MVC Project, I have faced an issue of relative path w.r.t server.
I am hosting the project as an application in IIS. Finally the url to my application will look like http://localhost/portal/Account/Login here 'portal' is the application name in IIS. In the ASP.net development server everything was working fine. while deploying it needed the relative path with respect to server. Because of this my jquery ajax requests started failing.
To fix this issue I kept the actions in hidden field and accessing from there and using for ajax request. The following is the code.
<input type="hidden" value="#Url.Action("GetNewOffersSearch", "Updates")" id="NewOffersSearchUrl" />
var NewoffersUrl = document.getElementById("NewOffersSearchUrl").value;
$.ajax({
type: 'GET',
url: NewoffersUrl ,
cache: false,
timeout: 10000,
contentType: "application/json; charset=utf-8",
success: function (_results) {
$("#updatesContent").html(_results);
},
error: function (_results) {
}
});
Initially NewoffersUrl was "/Updates/GetNewOffersSearch" and it was throwing a path error. But now it is "/portal/Updates/GetNewOffersSearch" and its working fine
I just want to know the approach I am following is correct or not. Is there any better fixes for this issue?
The way we do AJAX requests is similiar, however we pass the URL directly to the url parameter of the ajax call rather than using a hidden field.
$.ajax({
type: 'GET',
url: #Url.Action("GetNewOffersSearch", "Updates"),
cache: false,
timeout: 10000,
contentType: "application/json; charset=utf-8",
success: function (_results) {
$("#updatesContent").html(_results);
},
error: function (_results) {
}
});

javascript soap error calling asp.net web service

I have a working ASP.Net test web service, but I keep getting 500 errors as:
"System.InvalidOperationException: Request format is invalid: text/xml.
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
"
when I call it with javascript.
It is a simple web service that takes a single parameter as a string and returns it to the client. Please help!
link to code here
For those of you who this might help, the issue was setting the SOAPAction in teh header correctly:
$.ajax({
type: "post",
url: target,
contentType: "text/xml",
data: soapBody,
dataType: "xml",
processData: false,
beforeSend: function( xhr ){
xhr.setRequestHeader(
"SOAPAction",
"http://blahblah.com/Services/MethodName"
);
},
....
Make sure that your mess variable doesn't contain GET-style query string like '?a=1&b=2'. You need to send it in POST format, for example in JSON. Try to change contentType to contentType: "application/json; charset=utf-8"
$.ajax({
url: service,
type: "POST",
dataType: "xml",
data: '{key: value}',
complete: endTest,
error: processError,
contentType: "application/json; charset=utf-8",
});

Categories