I need to bring a list of numbers that are stored in a file into a web page. I wrote a web service with Visual Studio C# to read the file and respond with the contents. If I put the URL of the web service in a browser http:// MyComputer /wcfService3_deploy/RestServiceImpL.svc/json/123 it displays a json result: {"JSONDataResult":"9781770494138"} (there's only one number in the file).
Now, I need to call the service from my page. Unfortunately, the web service is on a different computer from my web page, so I need to use jsonp with ajax.
When I try to call the web service through javascript, it never gets to the service. I added an error parameter to the call and it reports "[object object] error"
function myFunction() {
var data = "datatext";
document.getElementById("response").innerHTML = "In MyFunction";
$.ajax({
type : "GET",
datatype : "jsonp",
url : "http://MyComputer/wcfService3_deploy/RestServiceImpL.svc/json/123?callback=jsonpCallback",
crossDomain : "true",
jsonpCallback : jsonpCallback(data),
success : jsonpCallback(data)
});
/*document.getElementById("response").innerHTML = "AfterAjax";*/
};
function jsonpCallback(data) {
document.getElementById("response").innerHTML =data;
};
</script>
<p id="response">Old Text</p>
<button type="button" onclick="myFunction()">Click Me!</button>
I know that I have jsonpCallback in here too many times, and that declaring data as a variable doesn't make sense. What is the difference between putting the callback in the url vs. the jsonpCallback parameter vs. the success parameter? If I don't declare data, it won't let me use it as an argument for the parameters. How does one specify the name of a variable that will contain the returned results?
Thank you for your patience
(data)from the two jsonpCallback in the $.ajax call