I have several views where I'm passing values to an external web service via an ajax post when the user clicks on submit or other action. For the url section of the ajax call I'm trying to pass a key from API.config, located in the Solution Items folder, where the value is different in each build/environment -
Solution Items folder layout
Key I'd like to pass to the ajax url: (API.config)
<appSettings>
<add key="RestApiEndpointBaseUrl" value="http://devserver/api/" xdt:Locator="Match(key)" xdt:Transform="SetAttributes"/>
Script within the view: (index.cshtml)
<script type="text/javascript">
var BaseUrl = '#System.Configuration.ConfigurationManager.AppSettings["RestApiEndpointBaseUrl"]';
$('#login').click(function () {
var note = {"Account":"#Model.AccountNumber","Text":"Log In","ActionCode":"CW","ResultCode":"LI","SequenceNumber":1,"UserId":"WEB"};
$.ajax({
type: "POST",
data: JSON.stringify(note),
url: BaseUrl + "Note",
contentType: "application/json"
});
});
</script>
Also, if I add "ToString()" to the end of the variable declaration like this -
var BaseUrl = '#System.Configuration.ConfigurationManager.AppSettings["RestApiEndpointBaseUrl"].ToString()';
I get a NullReferenceException error.
Otherwise, I get an empty string for the url. I'm not sure what I'm missing or doing wrong.
How, or Can I properly pass this as the url of the ajax call?
Related
I have strange problem because previously in my app this code was working but now isn't.
I take data from Laravel api via url address:
/api/customer/{id}/products
to select2 script
$('.js-data-example-ajax').select2({
ajax: {
url: '/api/customer/{id}/products',
dataType: 'json',
data: function (params) {
var query = {
q: params.term,
}
return query;
}
but Laravel make url address i that way:
api/customer/%7Bid%7D/products
so, I have %7Bid%7D instead of {id} and I'm looking for solution in google without success.
The character "7B" is { converter to asci, before the ajax request create a var call "url"
i call the route with his name, for give it the name just attach
->name('your_name') in the route file
and after in url variable i use the route name instead the full url
url = '{{ route("your_route_name", ":id") }}';
then replace the id placeholder with the id of select
url = url.replace(':id', id);
finally in ajax request
ajax: {
url: url,
//the rest of ajax request
}
How can I set s:param value in Ajax success body?
I load a data with Ajax call and I fetch it into datatable
but when I want to set s:param value I can not get its value, below is my code:
$.ajax({
url: "dataPp",
type: 'POST',
dataType : 'JSON',
success: function (res) {
table = $('table#dttable1').DataTable();
table.clear();
$.each(res.dokpp, function(i, item){
var json = res.dokpp[i];
table.row.add(
[json["kodeDok"],
json["fileNameUi"],
json["depPenerbit"],
json["createdDate"],
json["tglBerlaku"],
json["tglKadaluarsa"],
json["urutRev"],
'<s:url var="prev" namespace="/mr" action="prevDasboard">'+
'<s:param name="file">'+json["fileName"]+'</s:param>'+
'</s:url>'+
'preview'
]);
console.log(json["fileName"]);
});
table.draw();
}
});
I fixed this with this code :
'preview'
I use html tag to create a href.
When you use <s:param> tag the value is not yet available.
Struts tags are executed on server when JSP is rendered, but ajax is a javascript code which is executed on the client's browser after response is returned from the server. The server can't know what the value is substituted by the client.
You can render url without parameter and then add it dynamically.
var url = '<s:url var="prev" namespace="/mr" action="prevDasboard"/>?file='+json["fileName"];
I want to pass html content for example the body content to a wcf service through ajax call, its saying URL is too long. Can this be solved
Assign html content in javascript variable and use below code.
var html_content= "<html...content>";
$.ajax({
url: "ajax.php",//file_name
type: "POST",
data: {
content: html_content
},
});
I am working on Jquery AJAX in OXID eSHOP.
I want to pass proper action (function) name in AJAX url parameter.
Below is my code:
jQuery.ajax({
type: "POST",
data: {
variable: value
},
url: "",
success: function(data) {
}
});
I want to call one function in url parameter.
But don't know how to pass its value in url paramaeter.
Anyone knows then please help me.
You should include function name as URL parameter, just like the shop does it in most situations:
http://your-shop.com/index.php?cl=controller&fnc=functionname
So, I'm trying to return a hidden field (which is part of a form submission) which contains a string of html formatted text, there are reasons for this, the html is being rendered internally into pdf. I am currently posting the form back with ajax. The form serializes but when the form contains the html string, it returns a 500 error when trying to find the controller.
Code:
$(function () {
$('#preview').click(function (evt) {
//prevent the browsers default function
evt.preventDefault();
var $form = $('#sform');
$.ajax({
type: $form.prop('method'),
url: $form.prop('action'),
data: $form.serialize(),
dataType: "json",
traditional: true,
success: function (response) {
var newURL = window.location.protocol + "//" + window.location.host + "//" + response;
document.getElementById('myIframe').src = newURL;
}
});
});
});
Probably you need to turn off request validation. You can mark up your controller method with
[ValidateInput(false)]
Oh, and I think that for later versions of MVC you'll also need
<system.web>
<httpRuntime requestValidationMode="2.0"/>
...
</system.web>