2

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

1
  • you might want to remove the (data) from the two jsonpCallback in the $.ajax call Commented Apr 24, 2013 at 21:02

1 Answer 1

2

You have:

 jsonpCallback : jsonpCallback(data),
 success : jsonpCallback(data)

To expand on David's comment, your callback setup is saying that when building the callback, call jsonpCallback(data), and define "jsonpCallback" and "success" as the result of that function.

If you just provide it with the method to be called, it will apply it as a function to the returned object.

It would look like this:

jsonpCallback : jsonpCallback,
success : jsonpCallback
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.