we developed restful service using asp.net MVC4 web API,calling that service from PostMan is working fine,but will calling methods from Html page using JavaScript it shows error, plz any one help me
<script type="text/javascript">
$(function()
{
$("#btnShow").click(function()
{$.ajax({
type:"GET",
url: "http://eprescription.smartelectronicprescriptions.com/api/login",
contentType: "application/json; charset=utf-8",
data: '{}',
beforeSend: function (xhr)
{
xhr.setRequestHeader ("Authorization", "Basic", encodeunpwd());
},
success: function (msg)
{
alert("Success");
},
error: function (msg)
{
alert("error");
}
});
});
});
Because you are using a FQDN for the url, this is probably dropping in a jsonp request which is what is causing the error.
I can only assume this however as you haven't stated the error, have you tried looking at an example like this:
https://learn.jquery.com/ajax/working-with-jsonp/
It might help you get started on fixing it
Related
I have a laravel project. Each view page has it's on seperate js file. Problem is I am not able to access route functions from ajax post or get call in online server(digital ocean).
Error is:
Failed to load resource: the server responded with a status of 404 (Not Found)
The same application is working fine in my local system.
When I add the ajax function inside the design (blade.php) page it's working fine in server. It's not so pleasing to change all my functions inside design.
The server and application was working fine till yesterday. I made some changes in design and pulled it from bitbucket to server today. After that this error occurred.
Is there anything I have to do in server after pulling from bitbucket. I only pulled the changes made in design.
My route function:
Route::prefix('invoice_vno_bind')->group(function () {
Route::get('/', 'InvoiceController#getnextvno')->name('invoice_vno_bind');
});
AJAX function:
$.ajax({
type: "GET",
url: "invoice_vno_bind",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){
$('#txtVNo').val(data);
},
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.responseText);
}
});
Error function of ajax is executed when running in online server.
First, blade.php in add hidden parameter.
<input type="hidden" name="ajax_url" id="ajax_url" value="{{ route('invoice_vno_bind') }}">
And then get id="ajax_url" value in js file.
var url = $('#ajax_url').val();
$.ajax({
type: "GET",
url: url,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){
$('#txtVNo').val(data);
},
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.responseText);
}
});
Finally I got the error. It was because of some program lines in
/etc/nginx/sites-enabled/{your file name}
I commented out the codes below :
# location ~ /.(?!well-known).* {
# allow all;
# }
}
After commenting it worked fine. I don't know if this will affect any other fields. Till now it's perfectly working.
Thank you all for your suggestions.
I'd like to create a google-docs add-on that sends an ajax call to a webhook.
I've tried the below, but I get the following error
Error
ReferenceError: "$" is not defined. (line 5, file "")
Code
function myFunction() {
var arr = 'data'
$.ajax({
url: 'webhook_url',
type: 'POST',
data: arr,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
success: function () {
alert('Success');
}
});
}
If ajax can't be used here is there any other way to make a request to a server-side resource
This is an OLD question, so I don't know if you still have the issue, but, from the error you're getting Jquery is either not added, or not available.
You could try doing it with vanilla js, see this link for a walkthrough: https://www.sitepoint.com/guide-vanilla-ajax-without-jquery/
I have a strange issue using jQuery and JSON, especially JSONP.
My goal is to simply GET JSON data, but I always end up with the following error:
Uncaught SyntaxError: Unexpected token
Here is the code:
<script type="text/javascript">
$(document).ready(function() {
var myurl = "someurl";
$.ajax({
url: myurl,
method: 'GET',
contentType: 'application/javascript',
dataType : 'jsonp',
success: function(result){
//Do something with JSON result
}
});
</script>
And of course the JSON (raw format):
{"result":[{"targetView":"powerUsage","myData":{"someItems":["9","5","8"],"someItems2":[{"text":"protoText","currentRecord":"45.38","absolute":100}]}}]}
I tried the webservice with the Advanced Rest Client App in Google Chrome and it is working perfectly. I have no clue why this simple example gets this syntax error message.
Your Ajax code looks like fine. I think you are trying to make a Cross domain call as JSONP is a hack for dealing cross domain ajax call. If you Server code if ready for dealing with JSONP request then you must have send a callback parameter like
?callback=my_callback_method
than you service will return result with a callback see below links for more details:
https://learn.jquery.com/ajax/working-with-jsonp/
http://stackoverflow.com/questions/11736431/make-cross-domain-ajax-jsonp-request-with-jquery
You missed to put ready function close, that is }); at the last before script tag close:
<script type="text/javascript">
$(document).ready(function()
{
var myurl = "someurl";
$.ajax(
{
url: myurl,
method: 'GET',
contentType: 'application/javascript',
dataType: 'jsonp',
success: function(result)
{
//Do something with JSON result
}
});
});
</script>
I am trying to use JQuery to access Transloc's API (https://www.mashape.com/transloc/openapi-1-2#!documentation). I have not used JQuery or this API before. If anyone could shed some light on this I would appreciate it.
I can't test this as I don't have a key but it should work in theory.
Using the endpoint you linked to. Change the url for what you need.
$.ajax({
type:"GET",
beforeSend: function (request)
{
request.setRequestHeader("X-Mashape-Authorization", <mashape-key>);
},
url: "https://transloc-api-1-2.p.mashape.com/agencies.json?agencies=12%2C16&geo_area=35.80176%2C-78.64347%7C35.78061%2C-78.68218&callback=call",
processData: false,
success: function(data) {
//use data
},
error: function(data) {
//handle error
}
});
Our server is located in europe.
Now and then an american based user reports a problem when he uses the $.getJSON function.
His browser just displays the json response instead of catching it and passing it to javascript.
The ajax call just looks like:
$.getJSON(url, function(json_data){ ... });
Any ideas?
More info:
The the same user has the problem in FF and IE.
I use Ruby On Rails render :json. which response type is application/json.
Try using the $.ajax() method so you can handle errors and debug the success callback.
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
data: {},
dataType: "json",
url: url,
success: function(json_data) {
// parse json_data object
},
error: function (xhr, status, error) {
// check for errors
}
});
Aside from that using an XHR viewer like Firebug or Chrome's built-in utility (CTRL+SHIFT+I) can be very helpful.
XHR DOM reference: http://www.w3schools.com/dom/dom_http.asp