I have hosted a service sucessfully on IIS.
Made sure that application is running.
I have given call to this service through ajax as:
var parameters = {
'EmailID':EmailID,
'Password':Password
};
$.ajax({
url: "http://localhost:85/MobileECommerceTesting/Service1.svc/validateLogin",
data: JSON.stringify(parameters),
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "POST",
cache: false,
success: function (Data) {
alert("asdsad");
},
error: function () {
alert("Error in Saving.Please try later.");
}
});
But its not giving call to the service.
What can be the problem?
EDIT:
EDIT2 :
Network Tab:
Take a look at this question:
the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'
It might be related to your binding, which does not expect the content type application/json. You must use webHttpBinding to create "REST-like" services with WCF.
Related
I have a js when i do a ajax call to one server. When i call to this service using localhost, works. But when i call to this service using the server where i upload it, its ERROR 500. The call return a json.
$.ajax({
url: "https://www.example.com/example",
dataType: "json",
data: {
'data': xml,
'message': message,
'customer_id': customer_id,
'subscr_id': subscr_id
},
type: 'POST',
success: function (devol) {
},
error: function (xhr, ajaxOptions, thrownError) {
alert("no ha entrado");
}
});
Try to update your code below:
var model = {
'data': xml,
'message': message,
'customer_id': customer_id,
'subscr_id': subscr_id
};
$.ajax({
url: "https://www.example.com/example",
type: 'POST',
dataType: 'json',
data: JSON.stringify(model),
//headers: { 'authorization': `Bearer ${token}` },
async: false,
processData: false,
contentType: "application/json",
error: function (err) {
},
success: function (data) {
}
});
It might be because of CORS (Cross-Origin Resource Sharing)... Usually, you cannot make calls to other domains from the browser unless the other domain you are making calls allows CORS to all sites or specific sites.
enter link description here
Return error in the query
From the browser the answer is correct.
$.ajax({
type: "POST",
url: url,
async: true,
contentType: " charset=utf-8",
dataType: "XMLHttpRequest",
success: function (response) {
console.log(response);
},
error: function (msg) {
console.log(msg);
}
});
The message says "error".
I see three issues. First, dataType is a choice of xml, json, script, or html, unless you did something really fancy. jQuery can guess it based on received data though, so there is normally no need to set it. But if you want to be explicit (assuming your page returns json):
dataType: "json"
Second, contentType value looks like some truncated thing. I would just completely remove it, as you are not sending any data and just requesting a page.
Finally, when you are sending no data and just requesting a resource, the best is to use GET.
All in all:
$.ajax({
type: "GET",
url: url,
async: true,
dataType: "html",
success: function (response) {
console.log(response);
},
error: function (msg) {
console.log(msg);
}
});
When I am calling server method from AJAX call at that time showing 500 internal server error. And this error also happens sometimes only, while sometimes it is working fine.
I am really confused that what is going on that it is sometimes working and sometimes not working. In fact I didn't change anything after working the code but when I check second day it is coming this type of error.
Here is my code
<input type="button" id="btn_save" value="Save" class="button" />
$(document).on("click", "#btn_save", function (e) {
$.ajax({
type: "POST",
url: "schoolregistration.aspx/EntrySave",
data: JSON.stringify({ schoolName: $('#txt_schoolname').val() }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function () {
document.getElementById("txt_schoolname").value = "";
alert("Error! Try again...");
}
});
});
function OnSuccess(response) {
document.getElementById("txt_schoolname").value = "";
alert(response.d);
}
[WebMethod]
public static string EntrySave(string schoolName)
{
//Here is the code
}
Sometimes working fine but sometimes not coming call in this entrysave method.
try this:
$.ajax({
type: "POST",
url: "schoolregistration.aspx/EntrySave",
data: { "schoolName": $('#txt_schoolname').val() },
contentType: "application/json; charset=utf-8",
dataType: "json",
success:function (data) {alert('ok');},
error: function () {alert('error');}
});
I'm trying to consume a web service by the way of a method GET, but I can't get the success function, when the program goes to the error function, the alert displays "0", I don't know why.
Here's my code using AJAX
function funcAddPines() {
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "url.asmx/HelloWorld",
dataType: "json",
data: JSON.stringify({}),
success: function (objRes) {
alert("EntrĂ³ a web-service");
},
error: function (e) {
alert(e.status);
}
});
}
I am learning jQuery and trying the following but the parameters are so foreign to me with all the embedded quotes I think that is my problem. Can someone explain the parameters and where quotes go and possibly rewrite my parameters line? (This is a live site to see the required parms).
function AirportInfo() {
var divToBeWorkedOn = '#detail';
var webMethod = "'http://ws.geonames.org/citiesJSON'";
var parameters = "{'north':'44.1','south':'9.9','east':'22.4','west':'55.2','lang':'de'}";
$.ajax({
type: "POST",
url: webMethod,
data: parameters,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d);
$(divToBeWorkedOn).html(msg.d);
},
error: function(xhr) {
alert(xhr);
alert(xhr.statusText);
alert(xhr.responseText);
$(divToBeWorkedOn).html("Unavailable");
}
});
}
It looks like you are going to have a problem with the same origin policy.
In a nutshell, the policy prevents submitting an AJAX request across pages on different domains.
You should probably be using JSONP for Geonames, as described in the following Stack Overflow post:
JSONP callback fails, need help with javascript/jquery
Apart from that, you wouldn't have needed the single quotes here:
var webMethod = "http://ws.geonames.org/citiesJSON";
Try this way
var divToBeWorkedOn = '#detail';
var webMethod = "'http://ws.geonames.org/citiesJSON'";
var parameters = {'north':'44.1','south':'9.9','east':'22.4','west':'55.2','lang':'de'};
$.ajax({
'type': "POST",
'url': webMethod,
'data': parameters,
'contentType': "application/json; charset=utf-8",
'dataType': "json",
'success': function(msg) {
alert(msg.d);
$(divToBeWorkedOn).html(msg.d);
},
'error': function(xhr) {
alert(xhr);
alert(xhr.statusText);
alert(xhr.responseText);
$(divToBeWorkedOn).html("Unavailable");
}
});
I always use the way below. See if that works for you. I changed your code to be more like I'd do:
var divToBeWorkedOn = '#detail';
var webMethod = "http://ws.geonames.org/citiesJSON";
var parameters = { north:'44.1',south:'9.9', east:'22.4', west:'55.2',lang:'de' };
$.ajax({
type: "POST",
url: webMethod,
data: parameters,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d);
$(divToBeWorkedOn).html(msg.d);
},
error: function(xhr) {
alert(xhr);
alert(xhr.statusText);
alert(xhr.responseText);
$(divToBeWorkedOn).html("Unavailable");
}
});
i always write parameters like this:
data: "north=33.4&south=3"..... ,