简体   繁体   中英

jquery getjson is not working when calling restful webservice

I'm trying to call a restful webservice from my jsp page.

The restful web service is working fine and it is displaying the json data. The problem is with the client.
The client is not able to get the web service data.

I tried many ways but nothing worked.

This is the script:

function getEmployeeDetails() {
    alert("Hai");
    alert($("#empid").val());
    $empid = $("#empid");
    alert("eval-->" + $empid.val());
    $.getJSON("http://localhost:8080/RestfulWS/restful/employee/" + $empid.val(), displayResult);

}

function displayResult(data) {
    alert("data" + data);

    $("#fname").val(data.firstName);
    $("#lname").val(data.lastName);
}​

and the HTML

<form id="form1">
    <h2>Employee Details</h2>
    <table>
        <tr>
            <td>Employee ID : </td>
            <td><input type="text" id="empid" size="10"/> <input type="button"  value="Get Details" onclick="getEmployeeDetails()" /> </td>
        </tr>
        <tr>
            <td>FirstName : </td>
            <td><input type="text" id="fname" readonly size="30"/></td>
        </tr>
        <tr>
            <td>LastName :  </td>
            <td><input type="text" id="lname" readonly size="30"/></td>
        </tr>        
    </table>
</form>​

I am running the above code on tomcat with the port 19090.

Ok, Read HTTP access control , Cross-Origin Resource Sharing

and

How the origin is being determined

To configure your controller to allow cross domain requests,

This link will help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM