I am trying to call a web-service which has a Json output from JavaScript. But I am unable to get the value. I tried with different methods but have been unsuccessful. Please help !!
Here is the code I tried
<script src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
function callajax(){
var html =$.ajax({
cache: false,
type: "GET",
async: false,`enter code here`
data: {},
url: "http://domain/abc.php?param=abcd',
contentType: "application/json; charset=utf-8",
dataType: "json",
crossDomain: true,
Success: SucceedFunc,`enter code here`
error: function (request, errorText, errorCode)
{
alert(errorText+"--"+errorCode);
}
});
}
function SucceedFunc(data, status)
{
alert("Enter into Success");
alert(data);
}
Desired output is in {"name":Alex,"Success":true} format.
I need to pick value for "name".
Help would be appreciated.
Is it because your Success: is capitalized? Should be success:.
EDIT:
Also, is there a reason you're using such an old version of jQuery? They're at 1.9.x now (for most uses). I just looked it up and the crossDomain option was added in 1.5.
Related
I am writing a basic GET call using jquery AJAX in C# using MVC 4.
My code looks like this:
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "GET",
url: "/GetData",
content: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: function (success) {
var jsonData = success;
}
});
});
</script>
But I am getting following error:
Can someone please help me here. I have already spent a day with no luck yet.
how to get image url from "https://api.qwant.com/api/search/images?count=10&offset=1&q=cars" api using jquery. i'm unable to do it. bellow i've attached my code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$.ajax({
url:"https://api.qwant.com/api/search/images?count=10&offset=1&q=cars",
type:"GET",
crossDomain : true,
async: false,
dataType: "jsonp",
jsonpCallback: "myJsonMethod",
success: function(json) {
$.parseJson(json)
},
error: function(e) {
console.log(e);
}
});
function myJsonMethod(response)
{
console.log(response);
}
</script>
Your request isn't working because of CORS, which is enabled on the API server. You need a proxy server to workaround this. For development purposes you could use a free online proxy server, your code then simplifies:
$.ajax({
url:"<PROXY:SERVER>https://api.qwant.com/api/search/images?count=10&offset=1&q=cars",
success: function(json) {
// Do stuff with data
},
error: function(e) {
console.log(e);
}
});
As an example check out this working fiddle.
Try this
$.ajax({
url:"https://api.qwant.com/api/search/images?count=10&offset=1&q=cars",
type:"GET",
crossDomain : true,
async: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
jsonpCallback: "myJsonMethod",
success: function(json) {
$.parseJson(json)
},
error: function(e) {
console.log(e);
}
});
This is a CORS thing, so you can server all this up from a web server, like http-server, and I think certain browsers like Firefox allow this to occur locally.
There are some flags with Chrome that'd allow it to work locally like this too, I believe.
Cheers
I am trying to post json to an api. As well as get get html form data and convert it to json. But i cant make a simple ajax post and then return an alert box.
What is wrong with this?`
<script type="text/javascript">
$( document ).ready(function() {
$('#submit').click(function() {
$.ajax({
type: "POST",
url: "test url",
data: "hahaaha",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){alert(data);},
failure: function(errMsg) {
alert(errMsg);
}
});
});
});
</script>`
There's no failure function on the jQuery AJAX documentation, are you perhaps referring to error?
I've got a room monitor device I'm collecting data from, I can get it work with javascript but not jquery.
With plain javascript, define a function:
function myfunction(data){
console.log(data);
}
Then in the page:
<script type="text/javascript" src="http://172.16.198.19/getData.jsonp=callback=myfunction"></script>
I get an object in console containing all the data. Great!
I now try to get the same result using jQuery's $.ajax but am having problems:
$.ajax({
url: 'http://172.16.198.19/getData.jsonp',
dataType: 'jsonp',
jsonCallback: 'parseData',
success: function(data){
console.log(data);
},
error: function(){
console.log("nope");
}
});
This gives me the following error:
Uncaught ReferenceError: Invalid left-hand side in assignment
Any suggestions on what to try / how to fix are appreciated. Thanks.
Edit: Solved and answered. jQuery formats the query with _= in it which the server was rejecting. Working function is thus:
$.ajax('http://172.16.198.19/getData.jsonp', {
type: 'get',
dataType: 'jsonp',
jsonp: false,
jsonpCallback: 'parseData'
}).done(function(data) {
console.log(data.sensor[0].tc);
}).fail(function() {
console.log("nope");
});
}
This has been solved thanks to the very helpful Cork in #jquery on freenode.
The problem was jquery formatting the query with _= in it which the server was rejecting.
The working result is thus:
$.ajax('http://172.16.198.19/getData.jsonp', {
type: 'get',
dataType: 'jsonp',
jsonp: false,
jsonpCallback: 'parseData'
}).done(function(data) {
console.log(data);
}).fail(function() {
console.log("nope");
});
}
I have a JSON response coming from REST web service and want to bind that data to html using jQuery. Looks like its not even hitting web service url which I have provided in my jquery.
Url is working fine which gives me JSON data in browser but jQuery I am using unable to get any content from this. I am pasting my code here, plz let me know if some one can help.
While debugging script its directly going on error section in ajax call.
<script type="text/javascript">
$(document).ready(function () {
GetData();
});
function GetData() {
// alert(textblock.value);
$.ajax({
type: "GET",
url: "http://localhost:8092/api/Employees",
data: "{'employeeId'= '" + 1234 + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
var results = $.parseJSON(msg.d);
alert(msg);
alert(results);
},
error: function (result) {
alert('here');
var tt = result.text;
alert(tt);
}
});
}
</script>
finally i am able to get data now.
I added below properties in $.ajax:
processData: false,
async: false,