2016-10-05 3 views
0

Cette URL http://schooltray.com/VsStWsMblApps/SayHello?fullName=Joe%20Smithsimple appel jQuery Ajax ne fonctionne pas

profite de ce:

{"SayHelloResult":"{\"Status\":1,\"Message\":\"Hello Joe Smith\"}"}

Mais mon appel JQuery ne fonctionne pas. La page HTML complète est illustrée ci-dessous.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Say Hello</title> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
    <script type="text/javascript"> 
     $(function() { 
      $('#btn_SayHello').click(function (e) { 
       $.ajax({ 
        type: 'Get', 
        url: 'http://schooltray.com/VsStWsMblApps/SayHello?fullName=Joe%20Smith', 
        dataType: 'json', 
        success: function (response) { 
         alert('Success'); 
        }, 
        error: function (jqXHR, textStatus, errorThrown) { 
         alert('Error = ' + errorThrown + ' -- ' + jqXHR.responseText); 
        } 
       }); 
       e.preventDefault(); 
      }); 
     }); 
    </script> 
</head> 
<body> 
    Full Name:<br /> 
    <input id="txt_Email" type="text" style="width: 300px;" /> 
    <br /><br /> 
    <input id="btn_SayHello" type="button" value="Say Hello" /> 
    <br /><br /> 
</body> 
</html> 
+1

-vous faire une erreur? –

+0

Comment ça ne marche pas? – andrew

+0

Il tombe toujours dans la section d'erreur. et toutes les chaînes d'erreur sont vides. – GAksut

Répondre

1

Eh bien, j'ai testé dans mon local, Le code s'exécute correctement si vous utilisez un JSON local. Mais si vous voulez que le JSON à partir de cette URL perticular, je reçois, « Accès refusé »,

est Ci-dessous l'erreur,

XMLHttpRequest cannot load http://schooltray.com/VsStWsMblApps/SayHello?fullName=Joe%20Smith. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://cons.you.com' is therefore not allowed access.

-à-dire, vous êtes en train de faire un XMLHttpRequest à un autre domaine, où vous n'avez pas le contrôle d'accès. Par conséquent, le navigateur bloque la demande http.

0

Vous pouvez créer un formulaire et mettre toutes vos entrées à l'intérieur de cette forme et essayer

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Say Hello</title> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
    <script type="text/javascript"> 
     $(function() { 
      $('#btn_SayHello').click(function (e) { 
       $.ajax({ 
        type: 'Get', 
        url: 'http://schooltray.com/VsStWsMblApps/SayHello?fullName=Joe%20Smith', 
        dataType: 'json', 
        success: function (response) { 
         alert('Success'); 
        }, 
        error: function (jqXHR, textStatus, errorThrown) { 
         alert('Error = ' + errorThrown + ' -- ' + jqXHR.responseText); 
        } 
       }); 
       e.preventDefault(); 
      }); 
     }); 
    </script> 
</head> 
<body> 
<form name="formname"> 
    Full Name:<br /> 
    <input id="txt_Email" type="text" style="width: 300px;" /> 
    <br /><br /> 
    <input id="btn_SayHello" type="button" value="Say Hello" /> 
    <br /><br /> 
</form> 
</body> 
</html>