PDA

View Full Version : سوال: مشکل با فراخوانی وب سرویس در html



r_khan
دوشنبه 24 شهریور 1393, 09:44 صبح
با سلام
برای فراخوانی وی سرویس در html به صورت زیر عمل کردم ولی پاسخی دریافت نمیکنم



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<title></title>
<script type="text/javascript">





function Getdet(Name) {
$.ajax({
type: "POST",
url: "MyWebService/MyWebService.asmx/GetDateTimeService", // add web service Name and web service Method Name
data: "{}", //web Service method Parameter Name and ,user Input value which in Name Variable.
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {


// $("#spnGetdet").html(response.d); //getting the Response from JSON
document.getElementById("spnGetdet").innerHTML = response.d;


},
failure: function (msg) {
document.getElementById("spnGetdet").innerHTML = Name;
alert(msg);
}
});
}




</script>
</head>


<body>


<p>
<input id="Button1" type="button" onclick="Getdet('Name')" value="button" /><input id="TextBox1" type="text" /></p>

<p id="spnGetdet"></p>
</body>
</html>

nice_boy_a
دوشنبه 24 شهریور 1393, 10:40 صبح
سلام. آدرس وب سرویسو کامل بنویس
url: "http://localhost/YourSiteName/MyWebService/MyWebService.asmx/GetDateTimeService"

r_khan
دوشنبه 24 شهریور 1393, 10:45 صبح
سلام. آدرس وب سرویسو کامل بنویس
url: "http://localhost/YourSiteName/MyWebService/MyWebService.asmx/GetDateTimeService"

داخل iis گذاشتم

r_khan
دوشنبه 24 شهریور 1393, 10:51 صبح
اینم وب سرویس



using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;


namespace MyWebService
{
/// <summary>
/// Summary description for MyWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class MyWebService : System.Web.Services.WebService
{


[WebMethod]
public string HelloWorld()
{
return "Hello World";
}


[WebMethod]
public string GetAge(int year, int month, int day)
{
DateTime birthDate = new DateTime(year, month, day);
long age = new DateTime(DateTime.Now.Ticks - birthDate.Ticks).Year - 1;
return "You are " + age.ToString() + " years old.";
}


private const int CacheTime = 4; // seconds
[WebMethod(CacheDuration = CacheTime,
Description = "As simple as it gets - the ubiquitous Get Date Time.")]
public string GetDateTime()
{
return DateTime.Now.ToString();
}
}
}

ali.shahrivarian
دوشنبه 24 شهریور 1393, 11:07 صبح
مشکلتون مسیردهی و اسم تابع بود




function Getdet(Name) {
$.ajax({
type: "POST",
url: "/MyWebService/MyWebService.asmx/GetDateTime", // add web service Name and web service Method Name
data: "{}", //web Service method Parameter Name and ,user Input value which in Name Variable.
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {




// $("#spnGetdet").html(response.d); //getting the Response from JSON
document.getElementById("spnGetdet").innerHTML = response.d;




},
failure: function (msg) {
document.getElementById("spnGetdet").innerHTML = Name;
alert(msg);
}
});
}

r_khan
دوشنبه 24 شهریور 1393, 11:44 صبح
مشکلتون مسیردهی و اسم تابع بود




function Getdet(Name) {
$.ajax({
type: "POST",
url: "/MyWebService/MyWebService.asmx/GetDateTime", // add web service Name and web service Method Name
data: "{}", //web Service method Parameter Name and ,user Input value which in Name Variable.
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {




// $("#spnGetdet").html(response.d); //getting the Response from JSON
document.getElementById("spnGetdet").innerHTML = response.d;




},
failure: function (msg) {
document.getElementById("spnGetdet").innerHTML = Name;
alert(msg);
}
});
}


این الان خروجی داره ولی تابع خطا اجرا میشه.!!

ali.shahrivarian
دوشنبه 24 شهریور 1393, 11:47 صبح
خروجی خطا رو بذارید ببینم چیه

r_khan
دوشنبه 24 شهریور 1393, 11:49 صبح
خروجی خطا رو بذارید ببینم چیه

نمی دونم با چه کدی خطا رو برگردونم
این تابع اجرا میشه



failure: function (msg) {
document.getElementById("spnGetdet").innerHTML = Name;
alert(msg);
}

ali.shahrivarian
دوشنبه 24 شهریور 1393, 11:52 صبح
جای failure اینو بزار
اینجوری ارور میره تو کنسول inspector مرورگر


error:function(result){
console.log(result);
}

r_khan
دوشنبه 24 شهریور 1393, 12:00 عصر
جای failure اینو بزار
اینجوری ارور میره تو کنسول inspector مرورگر


error:function(result){
console.log(result);
}


500 (Internal Server Error)

r_khan
دوشنبه 24 شهریور 1393, 12:08 عصر
وقتی ادرس را به صورت http://localhost/MyWebService/MyWebService.asmx/GetDateTime وارد میکنم خطا بدین صورته



XMLHttpRequest cannot load http://localhost/MyWebService/MyWebService.asmx/GetDateTime. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

ali.shahrivarian
دوشنبه 24 شهریور 1393, 12:11 عصر
ارور اول از سرور هست. مسیر دقیق فایل .asmx رو به AJAX بدید. باز مشکل مسیردهی دارید.
وقتی میخواید درخواست بفرستید تو inspector برید تو تب network اونجا ارور رو کامل نشون میده و با کلیک کردن روش میتونید اطلاعات کاملش رو ببینید.

r_khan
دوشنبه 24 شهریور 1393, 12:22 عصر
ارور اول از سرور هست. مسیر دقیق فایل .asmx رو به AJAX بدید. باز مشکل مسیردهی دارید.
وقتی میخواید درخواست بفرستید تو inspector برید تو تب network اونجا ارور رو کامل نشون میده و با کلیک کردن روش میتونید اطلاعات کاملش رو ببینید.

ممنون که جواب میدی

این اخطارشه



Request format is unrecognized for URL unexpectedly ending in '/GetDateTime'.

ali.shahrivarian
دوشنبه 24 شهریور 1393, 12:23 عصر
خواهش...
team viewer داری؟

r_khan
سه شنبه 25 شهریور 1393, 09:41 صبح
مشکلات یکی پس دیگری حل شد
فقط الان یه مشکل هست
خطای زیر را دارم


Uncaught SyntaxError: Unexpected token <


اینم کد html


<html xmlns="http://www.w3.org/1999/xhtml"><head> <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script> <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.min.js"></script> <script> function serviceCall() { $.ajax({ type: 'POST', url: 'http://localhost/MyWebService/MyWebService.asmx/HelloWorld', data: '{}', contentType: 'application/json; charset=utf-8', dataType: 'jsonp', success: function (response) { $('#txtInput').val(response.d); console.log(response); }, error: function (result) { console.log(result); } }); }
</script></head> <body> <input id="Button1" type="button" onclick="serviceCall()" /> <input id="txtInput" type="text" /> <div id="divResult"> </div> </body></html>

ali.shahrivarian
سه شنبه 25 شهریور 1393, 09:51 صبح
یعنی اینکه مسیر فایلت رو اشتباه دادی و نمیتونه اسکریپت رو فراخونی کنه. احتمالا مربوط به jQuery میشه. مسیرات رو چک کن

r_khan
سه شنبه 25 شهریور 1393, 09:54 صبح
یعنی اینکه مسیر فایلت رو اشتباه دادی و نمیتونه اسکریپت رو فراخونی کنه. احتمالا مربوط به jQuery میشه. مسیرات رو چک کن

مسیر درسته روی خروجی پاسخ سرویس خطا میده
این پاسخ سرویس که روی خط 1 خطا میده!

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>

ali.shahrivarian
سه شنبه 25 شهریور 1393, 09:57 صبح
Uncaught SyntaxError: Unexpected token <

این خطا یعنی فایل لود نمیشه!

r_khan
سه شنبه 25 شهریور 1393, 10:15 صبح
Uncaught SyntaxError: Unexpected token <

این خطا یعنی فایل لود نمیشه!

این web config وب سرویسه شاید این مشکل داره؟!


<?xml version="1.0" encoding="UTF-8"?>


<configuration>

<appSettings />
<connectionStrings />

<system.web>


<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true" />
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows" />
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.


<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm"/>
<error statusCode="404" redirect="FileNotFound.htm"/>
</customErrors>
-->
</system.web>
<system.webServer>
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>

ali.shahrivarian
سه شنبه 25 شهریور 1393, 10:25 صبح
شما فایل jquery رو از سیستم خودت لود کن ببین چی میشه.

r_khan
سه شنبه 25 شهریور 1393, 11:10 صبح
مشکل حل شد باید در وب سرویس تگ [System.Web.Script.Services.ScriptService] را به بالای کلاس وب سرویس اضافه کنم.
تا وب سرویس به اسکریپ ها هم پاسخ دهد