I have created a web service which returns some data and am trying to access it using jquery but the console displays the following errors:
OPTIONS http://localhost:56018/PhoneWebServices.asmx?op=GetMyChildren 405 (Method Not Allowed)
jQuery.ajaxTransport.sendjquery-1.7.1.js:8102
jQuery.extend.ajaxjquery-1.7.1.js:7580
LoginButton_onclickindex.html:26
(anonymous function)index.html:59
onclickindex.html:60
XMLHttpRequest cannot load http://localhost:56018/PhoneWebServices.asmx?op=GetMyChildren. Origin null is not allowed by Access-Control-Allow-Origin.
I assumed the 405 error was due the ‘Origin null is not allowed’ error.
These are the steps I’ve taken so far:
- Created a website (work in progress) and created the web service in the website.
- I tested the web service by typing the url in my browser and it works.
- Created a mobile web app which tries to call the webservice but shows the above errors.
My Client-side (mobile app code):
<script type="text/javascript">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
}
function LoginButton_onclick() {
var email=document.getElementById("EmailBox").value;
var pass=document.getElementById("PasswordBox").value;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost:56018/PhoneWebServices.asmx?op=GetMyChildren",
data: '{ "email" : "' + email + '", "password": "' + pass + '" }',
dataType: "json",
success: GetChildrenSuccess,
failure: GetChildrenFailed
});
}
function GetChildrenSuccess(response) {
var children = eval('(' + response.d + ')');
var child;
for(child in children) {
$('#ResultsDiv').innerHTML = "ID: "+child.ID+ " Name: "+child.Name+" Surname: "+child.Surname+" \r\n";
}
}
function GetChildrenFailed(error) {
document.getElementById('ResultsDiv').innerHTML = "Error";
}
</script>
I was thinking maybe it was because I did not publish the website or webservice with IIS-do I need to do this? Even though the url worked when typed into the browser I am not sure whether it should be the same in the client side code.
I am very new to web programming so if you know what’s wrong please explain it in simple terms, any help is greatly appreciated.
The problem is most likely to go away if you run the file through a local web server, and not just open the HTML-file with the browser. When you just open the file, without going through a web server, you don’t get an origin domain – thus the error message that the origin is null.