Hi i tried to call a rest webservice from ajax get request.
Following is the code i tried for that.
function callRestService(){
var xmlhttp;
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
$.ajax({
xmlhttp.open("GET","http://mywebservice/test/",false);
xmlhttp.send();
alert(xmlhttp.responseText);
});
}
And following error am getting while running this code/
missing : after property id
[Break On This Error]
xmlhttp.open("GET","http://mywebservice/test..
/AjaxC...ervice/ (line 30, col 13)
In the first case i tried something like following
$.ajax({
type: "GET",
url: "http://mywebservice/test/",
cache: false,
success: function(data){
alert(data);
//var obj = eval(data);
},
error: function (msg, url, line) {
alert('error trapped in error: function(msg, url, line)');
alert('msg = ' + msg + ', url = ' + url + ', line = ' + line);
}
});
and in the above case control comes into the error block, but i didn't get what's the reason for that .and that's why i tried the first case.
Is there any problem with this code..??Can anyone help in this.?
Your code is all wrong.
Assuming that$.ajax is jQuery ajax call, then Your code should look like this:
function CallRestService() {
$.ajax({url:'http://mywebservice/test'}).done(function(data) {
alert(data);
})
);
}
You don't need to create xml http request if you're using jquery ajax call.
See this: http://api.jquery.com/jQuery.ajax
for reference.
If you don't want to use jQuery:
function callRestService(){
var xmlhttp;
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://mywebservice/test/",false);
xmlhttp.send();
if (xmlhttp.status == "200") {
alert(xmlhttp.responseText);
}
}
Reference here: Using XMLHttpRequest
If you are using cross domain calls, see here:
jQuery AJAX cross domain
Related
please help bring an array of data. spring is here:
{"news": [{"img": "http://static2.smi2.net/img/160x120/2212764.jpeg", "title": "Усиление слуха в 2 - 3 раза! Уникальная разработка", "url": "http://news.smi2.ru/newdata/news?ad=681120&bl=81060&ct=adpreview&st=16&in=lJUrBQDHL4CgZAoA", "id": "681120"}]}
I do the following:
var massive = JSON.parse('http://news.smi2.ru/data/js/81060.js');
console.log(massive.news.id);
console.log(massive.news.img);
console.log(massive.news.title);
console.log(massive.news.url);
The result is the following error message:
Uncaught SyntaxError: Unexpected token h
use only the native js
You try to parse an url and not a json. You can pass the respone you get and parse it using JSON.parse:
var massive = JSON.parse('{"news": [{"img": "http://static1.smi2.net/img/160x120/2269036.jpeg", "title": "Украинские власти и Саакашвили прокомментировали убийство Немцова", "url": "http://news.smi2.ru/newdata/news?ad=696406&bl=81060&ct=adpreview&st=16&in=uU6IBQAiL4BWoAoA", "id": "696406"}]}');
console.log(massive.news[0].id);//outputs 696406
To clear it up you can make an request to the url you want for example using an ajax call:
$.ajax({
url: "http://news.smi2.ru/data/js/81060.js",
dataType : "json"
}).done(function(res) {
console.log(res.news[0].id);//outputs 696463
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
You cannot parse Json from url .If you need data from URI do send an ajax call for that :-
//Simple javascript example.
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var massive = JSON.parse(xmlhttp.responseText);
console.log(massive.news[0].id);
console.log(massive.news[0].img);
console.log(massive.news[0].title);
console.log(massive.news[0].url);
}
}
xmlhttp.open("GET","http://news.smi2.ru/data/js/81060.js",true);
xmlhttp.send();
I'm working in an app and I was catching all the ajax request using:
$(document).ajaxComplete(function (xhr, status) {
//...
});
Now, I'm using MicrosoftMvcAjax and MicrosoftAjax and when a XHR request finished a message in the console says: XHR finished loading.
How I can catch when a xhr request finish or what is the equivalent for ajaxComplete in xhr?
Use a counters
var _complete = 0, _on = 0;
function incr(){ _on++; }
function comp(){ _complete++; if(_complete==_on) console.log("All the completed")}
// Now use these function wherever you want to make an AJAX Call like
incr();
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
comp();
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
function search_val() {
$("#company_code_nr1").click();
$("#poc").click();
$("#company_code_nr").click();
$("#subcompany_code_nr").click();
$("#cs_code_nr").click();
$("#invoice_id_nr").click();
$("#user_code_tx").click();
$("#owner_code_nr").click();
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
var str=$("#company_code_nr").val();
var str2=$("#subcompany_code_nr").val();
var str3=$("#product_code_nr").val();
var str4=$("#invoice_code_nr").val();
var url="hotkeydetail.php?f3="+str+"&c4="+str2+"&c6="+str3+"&c7="+str4;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
hi i am using code for getting content on another page every things is OK Mozilla and other browser also work on IE 10 . but not responding on ie8 and ie9. please help me. thanks
Use jQuery Ajax like this:
function search_val() {
$("#company_code_nr1").click();
$("#poc").click();
$("#company_code_nr").click();
$("#subcompany_code_nr").click();
$("#cs_code_nr").click();
$("#invoice_id_nr").click();
$("#user_code_tx").click();
$("#owner_code_nr").click();
var str=$("#company_code_nr").val();
var str2=$("#subcompany_code_nr").val();
var str3=$("#product_code_nr").val();
var str4=$("#invoice_code_nr").val();
$.ajax({
type: 'GET',
url: 'hotkeydetail.php',
data:{'f3':str,'c4':str2,'c6':str3,'c7':str4},
success: function(result){
$('#txtHint').html(result);
}
});
}
From my previous question(Create json using JavaScriptSerializer), In .ashx file I am printing the json object using:
context.Response.ContentType = "application/json";
context.Response.Write(json);
I am calling this .ashx file from default.aspx which has some javascript function inside its <head> tag.
My question is :
How will I be able to call the javascript function from .ashx file after context.Response.Write(json);?
UPDATE:
My ultimate goal is to achieve Server Side Processing for DataTable.In that i want to bind the rows with context menu using javascript function.
For that I am using following code to call .ashx file:
$('#example').dataTable({
'bProcessing': true,
'bServerSide': true,
'sAjaxSource': '/data.ashx'
});
Are you using ajax requests? In that case, you can use the success method that is available in javascript, as in the following example from w3schools:
function showHint(str)
{
var xmlhttp;
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
// You can call your custom method here...
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","gethint.asp?q="+str,true);
xmlhttp.send();
}
Or if you are using jquery:
$.ajax({
url: "test.html",
context: document.body
}).done(function() {
// You can call your custom method here...
$(this).addClass("done");
});
UPDATE
Check out: http://datatables.net/usage/callbacks The method you can use is: fnInitComplete
e.g.
$('#example').dataTable({
'bProcessing': true,
'bServerSide': true,
'sAjaxSource': '/data.ashx',
'fnInitComplete' : function() {
alert('Your menu population code here!');
}
});
You could use
eval
to evaluate the response as javascript on client-side. But I doubt you really need or want this, it might not be a very elegant solution. So what you want to archieve?
I am trying to load an XML file that asks for a username and password.
The code I'm using is as follows that I am trying to send the username and password in the URL:
xmlhttp.open("POST","http://admin:admin#192.168.0.5/myfile.xml",false);
The full code of my page looks like this:
<html>
<body>
<textarea id="test1" name="test1" cols="90" rows="30">
XML file will be displayed here
</textarea><br>
<script type="text/javascript">
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("test1").value=xmlhttp.responseText;
} else
{
alert('Panel not communicating.Reason: '+xmlhttp.status);
}
}
xmlhttp.open("POST","http://admin:admin#192.168.0.5/myfile.xml",false);
xmlhttp.send();
</script>
</body>
</html>
Anyone know what I am doing wrong?
First, add this function to create the authorization header contents:
function make_base_auth(user, password)
{
var tok = user + ':' + pass;
var hash = Base64.encode(tok);
return "Basic " + hash;
}
Then, call the function and store the return value in a separate variable:
var auth = make_basic_auth('admin', 'admin');
Lastly, pass the value of auth to the XMLHttpRequest object:
xmlhttp.setRequestHeader('Authorization', auth);
xmlhttp.send();
Attribution: http://coderseye.com/2007/how-to-do-http-basic-auth-in-ajax.html
I ended up getting it working by doing the following:
Change:
xmlhttp.open("POST","http://admin:admin#192.168.0.5/myfile.xml",false);
To:
xmlhttp.open("POST","http://admin:admin#192.168.0.5/myfile.xml",false,"username","password");
See this question for a similar scenario. How to use Basic Auth with jQuery and AJAX? Note one answer states that IE does not support credentials in a URL (as you are attempting to do).