简体   繁体   English

使用ajax()和jsonp调用asp.net远程Web服务.asmx时出现意外令牌<

[英]unexpected token < when calling asp.net remote webservice .asmx using ajax() and jsonp

I have a very basic - for testing - ASP.net Web service (2.0 and IIS 6.0) written in VB running on a remote server 我有一个非常基本的-用于测试-用VB编写的ASP.net Web服务(2.0和IIS 6.0)在远程服务器上运行

WEB SERVICE 网络服务

 <WebService(Namespace:="CMS_ChecklistSystemWebService")> _
 <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
 <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
 <System.Web.Script.Services.ScriptService()> _
 Public Class CMS_ChecklistSystemWebService
    Inherits System.Web.Services.WebService
    <WebMethod()> _
    Public Function HelloWorld() As String
        Return "Hello World"
    End Function

CONFIG. CONFIG。

i had to add these following lines to my webservice config in order to be able to run the webservice in a Browser 我必须将以下几行添加到我的webservice配置中,以便能够在浏览器中运行该webservice

<system.web>
    <webServices>
        <protocols>
            <add name="HttpGet"/>
            <add name="HttpPost"/>
        </protocols>
    </webServices>
</system.web>

**WEB SERVICE OUTCOME ** **网络服务成果** 在此处输入图片说明

I was trying to call this web service from PHP page using AJAX JQUERY running XAMPP v3.1 我试图使用运行XAMPP v3.1 AJAX JQUERYPHP页面调用此web service

JQUERY JQUERY

  $.ajax({
        type: "GET",
        url: "http://192.168.25.11/link to web service",
            data: "",
            dataType: "jsonp",
            contentType: "application/jsonp; charset=utf-8",
            success: function(data) {
                console.log(data);
            }
        });

ERROR 错误

Following is how my console looks like after calling the ajax() function 以下是调用ajax()函数后控制台的外观 在此处输入图片说明

FYI if i click on the link in the second line it will link you to the webservice and run it as in the first image 仅供参考,如果我在第二行点击它会连你的webservice并运行它作为第一个图像

NOTICE 注意

if i use the same webservice from the same project - domain - i can easily do ajax() call and type='json' and it works perfectly with no errors 如果我从同一个项目(域)使用同一个webservice ,则可以轻松地执行ajax()调用并type='json'并且它完美地运行且没有错误

Question

  • What i'm doing wrong ? 我做错了什么?
  • Do i need to make the Webservice return a JSON object instead of XML if yes then how 我是否需要做Webservice返回JSON而不是对象XML ,如果是,那么如何

I think the problem is, you are specifying content type as json in ajax call. 我认为问题是,您在ajax调用中将内容类型指定为json。 But, your web service is returning xml. 但是,您的Web服务正在返回xml。

So, either you can specify content type as xml in ajax call and process the xml (I am not sure whether it is possible or not), or you can configure web service to return json. 因此,您可以在ajax调用中将内容类型指定为xml并处理xml(我不确定是否可以),也可以配置Web服务以返回json。

Refer this SO question ( How to return JSON from a 2.0 asmx web service ) to configure web service to return json. 请参阅此SO问题( 如何从2.0 asmx Web服务返回JSON )以将Web服务配置为返回JSON。

Rumit has, I think, only given part of the answer here. 我认为,Rumit仅给出了部分答案。 You certainly need to set your web service to return JSON instead of XML. 当然,您需要将Web服务设置为返回JSON而不是XML。

However, you've stated that you're setting dataType: "jsonp" This requires that you return your JSON inside a method call with the name of the method being the value of the callback key in your second image above. 但是,您已经声明要设置dataType: "jsonp"这要求您在方法调用内返回JSON,并且该方法的名称为上面第二张图片中的callback键的值。

for example, it would look something* like this : 例如,它看起来像*:

jQuery1910366312976758182({data: "Hello World"})

*not exactly, but step through and you'll get the idea... *不完全是,但逐步执行,您将了解...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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