简体   繁体   English

jQuery移动Web应用程序开发(需要帮助)

[英]jquery mobile web application developement( need help)

Can any one please tell me how to send data to web service using jquery and receive data from web service? 谁能告诉我如何使用jquery将数据发送到Web服务以及如何从Web服务接收数据? If we are using web service should we need to use url to get records? 如果我们使用的是Web服务,是否需要使用url来获取记录?

$j.ajax({
        type: "GET",
        url: "testing.json",
        dataType :'json',
        contentType:'application/json; charset =utf-8',
        success:function(data)
        {
        $j.each(data, function(index,element){
            $j('#json').append("<li class='ui-li ui-li-static ui-btn-up-c ui-corner-top ui-corner-bottom ui-li-last'>"+element+"</li>");
         });
        }
    })
});

I am developing web application using jQuery mobile. 我正在使用jQuery mobile开发Web应用程序。

Can any one please tell me how to send data to web service using jquery 谁能告诉我如何使用jquery将数据发送到Web服务

Put it in a data property on the object you pass as the first argument to ajax() . 将其放在作为ajax()的第一个参数传递的对象的data属性中。

How you format the data will depend on the particular web service. 数据格式化的方式将取决于特定的Web服务。

Your existing code claims that it will be JSON so the data you pass to data should be a string representation of a JSON Text. 您现有的代码声称它将是JSON,因此传递给data应为JSON文本的字符串表示形式。

You will need to change the type to POST in order to do this though. 不过,您需要将类型更改为POST才能执行此操作。 The content-type request header describes the request body and you don't get one of those with a GET request. content-type请求标头描述了请求正文,您不会从GET请求中获得其中之一。

(If the web service does not expect to receive JSON data then you will need to change the code to represent whatever it does expect). (如果Web服务不希望接收JSON数据,那么你将需要修改代码来表示任何它希望)。

and receive data from web service? 并从Web服务接收数据?

Read it from the first argument to the callback function you pass to the success function. 从第一个参数到传递给success函数的回调函数中读取它。

If it is in a known data format (XML, HTML or JSON), then jQuery should automatically parse it. 如果它采用已知的数据格式(XML,HTML或JSON),则jQuery应该自动解析它。 Note that you have dataType: 'json' which will override whatever the server says it is sending back and try to parse it as JSON data regardless. 请注意,您具有dataType: 'json' ,它将覆盖服务器说回发的所有内容,并尝试将其解析为JSON数据。

If we are using web service should we need to use url to get records? 如果我们使用的是Web服务,是否需要使用url来获取记录?

Yes. 是。 URLs are how web server end points are identified. URL是如何标识Web服务器端点的。

a liitle example to get data from a web service using jquery ajax call 一个使用jquery ajax调用从Web服务获取数据的小例子

function GetData() {

    $.ajax({
        type: "POST",
        url: "Members.asmx/GetMemberDetails",//your webservice call
        data: "{'MemberNumber': '" + $("#txt_id").val() + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnGetMemberSuccess,
        error: OnGetMemberError
    });
}

function OnGetMemberSuccess(data, status) {
    //jQuery code will go here...
}

function OnGetMemberError(request, status, error) {
    //jQuery code will go here...

 }

Example: Introduction to using jQuery with Web Services 示例: 将jQuery与Web服务结合使用的简介

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

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