Javascript vb2013 calling url removing host name - javascript

I am new to serious javascript programming, Working my way up in the world to HTML5 and I have gone round and round.
The address works in a browser so I know its there, but when I go to debug I get a 404 error from the following javascript code
function loadTotalLabels() {
//get json data
var jsonData = JSON.stringify({ "args": "" });
$.ajax({
type: "POST",
async: false,
cache: false,
url:"http://localhost:52594/Service1.svc/getTest",
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json", // dataType is json format
success: onTotalsSuccess_,
error: OnErrorCall_
});
}
When I look at the error it tells me that it can not find Service1.svc/getTest
If I remove the Port number and put the project name in (http://localhost/prsTest/service1.svc/getTest which is running on my local IIS), it works fine (throws a Network error, but one error at a time).
So, can anyone tell me how to get the Javascript to use the "localhost + port number"? I have tried location.host and that did not work. Or tell me where to look, as I have not found anything on this item.

Related

jQuery sending undefined "_" parameter

I am trying to write a script that gets COVID-19 numbers from the Ontario Database, but I keep getting the following error:
"invalid value \"_\""
Upon further investigation, the URL that is trying to be accessed contains the following parameter:
&_=1610496351832
As you can see in my code below, I never define such a variable:
var data = {
resource_id: '8a89caa9-511c-4568-af89-7f2174b4378c' // the resource id
};
$.ajax({
dataType: 'jsonp',
data: data,
url: 'https://data.ontario.ca/api/3/action/datastore_search'
});
Is there any way I can remove the _ object from the request?
The file I am trying to access is located at the following URL, where data is the resource_id.
https://data.ontario.ca/api/3/action/datastore_search?resource_id=8a89caa9-511c-4568-af89-7f2174b4378c
That is an automatic value generated by JQuery to avoid the cache.
Add the following to your script if you want to get rid of the underscore param:
var data = {
resource_id: '8a89caa9-511c-4568-af89-7f2174b4378c' // the resource id
};
$.ajax({
dataType: 'jsonp',
data: data,
type:'GET',
cache: true,
url: 'https://data.ontario.ca/api/3/action/datastore_search'
});
It only occurs in GET requests, you can play around with this setting. But remember, the automatic underscore param is included intentionally to avoid the request cashing.

Not able to get data that is in Bengali language from server using jQuery AJAX

I am not able to get data that is in Bengali language from server using ajax. The data from server is getting replaced by something I don't know. But if I normally set the data means not using ajax, data is getting displayed properly. My code is:
<meta content="html/text" charset="UTF-8"> in .jsp file
$.ajax({
url: "",
data: "a=a",
contentType: "charset=utf-8",
method: "get",
success:function(){
},
failure:function(){
}
});
I have taken reference from UTF8 encoding not working when using ajax, but even this did not work.
The error is in the ajax statement config.
contentType="charset=utf-8",
Here, you should use : instead of =.
See the error highlighted in the code below.
$.ajax({
url: "YOUR URL HERE?myparam1=value1&param2=value2",
data: "a=a",
contentType: "charset=utf-8",
// ^
method: "GET",
success: function(resp) {},
failure: function(err) {}
});

jQuery Ajax POST JSON issue

I'm trying to pass a JavaScript array into a PHP script to then be put into a database but I'm having issues. I have used JSON.stringify on the array before sending. The request seems to work in that when stepping through the code debugging the php page is called but no data is passed into the POST global. I'm sure its something amateurish that I've missed but I'm struggling. This is the code:-
$.ajax({
type: "POST",
datatype: "json",
url: "processdraw.php",
data: {
json: pDrawnTeams
},
contentType: "application/json; charset=utf-8",
success: alert('worked')
})
If you are not getting any error in your javascript, make sure that you are getting that parameter like this in your php target file:
$myJSON = $_POST['json']; // ['parameter name']
after that you will need to decode that json
$myData = json_decode($myJSON, true) // true is for retrieving as an array

double controller value in URL call by ajax

I got a strange problem. while i run my VS and click specific button on browser, an ajax function fired and shows error. After debugging i found the URL is showing error. the error is::
POST http://localhost:4942/Employee/Employee/AllEmployees 404 (Not Found)
The problem is, for some reason the "/Employee" controller is coming two times.
my ajax call is:
function allEmployeeFunc() {
$.ajax({
type: "POST",
url: "Employee/AllEmployees",
//data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
// context:"" ,
error: function (msg) {
alert("something is wrong");
},
success: function (data) {
}
});
}
Here the URL clearly showing only one /Employee. so whats the problem?? can anyone help please??
Try to add a slash to the URL
url: "/Employee/AllEmployees"
I guess you are using too much in url; I can see "/Employee/Employee/AllEmployees". Employee twice. Rather try
url: "AllEmployees"
I guess that should do. Assuming that you have annotation [HttpPost] in place to hit AllEmployees function.

getting json string from web service

I have got a web service which gets list of users from an external system and returns as json. and I call that webservice via jquery ajax.; I have placed ajax code below
$.ajax({
type: "GET",
url: webMethod,
data:"",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
success: function(msg) {
alert(msg.d);
},
error: function(e) {
alert(e);
}
});
Even though, the output is in correct format, the output I get from jquery.ajax seems to be wrong. it returns big chunk of the data correctly, then adds "; (" and continues to show the output.
Basically, the output is ("around %75 of data");(rest of the data) which makes my json invalid. I am not sure whether it is related to the maxJasonLenght or not but I also set it to the maximum. there seems to be a limitation on how much data you can get from web service as if I add more data to that json, the break down point changes.
Sample Output
[{"UserName":"a.b","FullName":"a b"},{ Many other users},{"UserName":"c.d","FullName":"c d"},{"UserName":"e.f",);jsonp1364397526212("FullName":"e f"}, {"UserName":"g.h","FullName":"g f"},{other users}}
do you have any idea why I am having this issue?
Thanks
Do you set the crossDomain option to TRUE? If I'm not wrong, if you set the crossDomain option to TRUE, the response would be JSON-P.
Look at this post so you can figure out how to handle the response:
What is JSONP all about?
I hope it would help!

Categories

Resources